现在有一张表,如下图:
此主题相关图片如下:2.jpg
我想生成如下图的汇总表:
此主题相关图片如下:1.jpg
尝试了用手动编码的方式这样做的:
Dim dtb As New DataTableBuilder("统计")
dtb.AddDef("商户编号", Gettype(String), 16)
dtb.AddDef("商户名称", Gettype(String), 64)
dtb.AddDef("期初库存", Gettype(Double))
dtb.AddDef("入库量(吨)_汽运入库", Gettype(Double))
dtb.AddDef("入库量(吨)_铁运入库", Gettype(Double))
dtb.AddDef("入库量(吨)_转库入库", Gettype(Double))
dtb.AddDef("入库量(吨)_入库合计", Gettype(Double))
dtb.AddDef("出库量(吨)_汽运出库", Gettype(Double))
dtb.AddDef("出库量(吨)_铁运出库", Gettype(Double))
dtb.AddDef("出库量(吨)_转库出库", Gettype(Double))
dtb.AddDef("出库量(吨)_出库合计", Gettype(Double))
dtb.AddDef("期末库存", Gettype(Double))
Tables("出入转汇总表_Table1").DataSource = dtb.BuildDataSource()
Dim cmd As New SQLCommand
cmd.C
Dim dt As DataTable
cmd.CommandText = "Select * From {出入转流水}"
dt = cmd.ExecuteReader
For Each nm As String In dt.GetValues("商户编号")
Dim dr As DataRow = DataTables("出入转汇总表_Table1").AddNew()
cmd.CommandText = "Select * From {商户库存} Where 商户编号 = '" & nm & "'"
Dim dt1 As DataTable = cmd.ExecuteReader
dr("商户编号") = nm
dr("商户名称") = dt1.DataRows(0)("商户名称")
dr("期初库存") = dt1.Compute("Sum(库存数量)")
dr("入库量(吨)_汽运入库") = dt.Compute("Sum(重量)","[商户编号] = '" & dr("商户编号") & "' And [运输方式] = '汽运' And [出入转] = '入'")
dr("入库量(吨)_铁运入库") = dt.Compute("Sum(重量)","[商户编号] = '" & dr("商户编号") & "' And [运输方式] = '铁运' And [出入转] = '入'")
dr("入库量(吨)_转库入库") = dt.Compute("Sum(重量)","[商户编号] = '" & dr("商户编号") & "' And [运输方式] = '转入'")
dr("入库量(吨)_入库合计") = dt.Compute("Sum(重量)","[商户编号] = '" & dr("商户编号") & "' And [出入转] = '入'")
dr("出库量(吨)_汽运出库") = dt.Compute("Sum(重量)","[商户编号] = '" & dr("商户编号") & "' And [运输方式] = '铁运' And [出入转] = '出'")
dr("出库量(吨)_铁运出库") = dt.Compute("Sum(重量)","[商户编号] = '" & dr("商户编号") & "' And [运输方式] = '转出' And [出入转] = '出'")
dr("出库量(吨)_转库出库") = dt.Compute("Sum(重量)","[商户编号] = '" & dr("商户编号") & "' And [运输方式] = '转出'")
dr("出库量(吨)_出库合计") = dt.Compute("Sum(重量)","[商户编号] = '" & dr("商户编号") & "' And [出入转] = '出'")
dr("期末库存") = dr("期初库存") + dr("入库量(吨)_入库合计") - dr("出库量(吨)_出库合计")
Next
因为上面这种方式的效率实在不敢恭维,就考虑了下面的方式:
Dim g As New SQLCrossTableBuilder("统计表1", "出入转流水")
g.C
g.HGroups.AddDef("商户编号")
g.HGroups.AddDef("商户名称")
g.HGroups.AddDef("园区")
g.VGroups.AddDef("出入转", "{0}库量(吨)")
g.VGroups.AddDef("运输方式", "{0}")
g.Totals.AddDef("重量", "重量")
g.OrderByTotal = True
g.HorizontalTotal = True
g.FromServer = True
g.Build()
MainTable = Tables("统计表1")
但是问题来了!
首先我想实现期初库存的调用怎么办?
可以用直接进行汇总统计这个流水表实现!这个也可以理解,但是不知道流水表几十万条数据全部进行统计会不会效率很低?
另外:我的表里面有个叫园区的列,也就是说,再不同园区,有相同的商户名称存在,我需要对相同的商户名称进行合并分组,合并后还要再期末库存的后面增加一列,把相同商户的不同园区的数量进行合计,求方法!
附下图样式!
此主题相关图片如下:3.jpg