|
SQL使用Sum(大小),Sum(Round(大小,1)),Sum(Round(大小,0)),Sum(int(大小))误差
如图所示。
- Function SqlRetuRs(Str)
- Dim Cn As ADODB.Connection
- Set Cn = New ADODB.Connection
- Dim Rs As ADODB.Recordset
- Set Rs = New ADODB.Recordset
- Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName
- Rs.Open Str, Cn, adOpenKeyset, adLockOptimistic
- Set SqlRetuRs = Rs
- End Function
- Sub lll2()
- Dim Str, Rr
- Dim Sht As Worksheet
- Dim Rng As Range, oRng As Range, oRng1 As Range
- Set Rng = Selection
- Set Sht = Rng.Parent
- Set Rng = Sht.Cells(20, 1).CurrentRegion
- Rr = Rng.Row + Rng.Rows.Count
- Set oRng = Sht.Range("A1:G" & Rr)
-
- Set oRng1 = Sht.Cells(Rr + 20, 1).Resize(30000, 24)
- oRng1.Select
- oRng1.ClearContents
-
- Dim Rs As Recordset, Rs1 As Recordset, Rs2 As Recordset
-
-
- Str = "Select 地点,Count(地点),Sum(大小),Sum(Round(大小,1)),Sum(Round(大小,0)),Sum(int(大小)) From [" & Sht.Name & "$" & oRng.Address(0, 0) & "] Where 地点 <> Null Group by 地点 "
- Set Rs = SqlRetuRs(Str)
- Sht.Cells(Rr + 20, "B").CopyFromRecordset Rs
- Set Rng = Sht.Cells(Rr + 21, "B").CurrentRegion
- Debug.Print Rng.Address
-
- Rng.Select
- ss1 = Application.WorksheetFunction.Sum(Rng(, 2).Resize(Rng.Rows.Count, 1))
- ss2 = Application.WorksheetFunction.Sum(Rng(, 3).Resize(Rng.Rows.Count, 1))
- Str = Sht.Name & vbCr & "小计" & vbCr & "共" & ss1 & "张,合计" & ss2 & "MB"
- Debug.Print Str
- Stop
- Stop
- Stop
- End Sub
复制代码
|
|