参考代码
Dim month As Integer = 2
Dim dtb As New DataTableBuilder("统计")
dtb.AddDef("产品名称", Gettype(String), 16)
Dim prds As List(of String) = DataTables("test").GetValues("部门")
Dim sum_y As String = ""
Dim sum_n As String = ""
For Each prd As String In prds
sum_y &= "isnull(" & prd & "_本月,0)+"
sum_n &= "isnull(" & prd & "_本年,0)+"
dtb.AddDef(prd & "_本月", Gettype(Double))
dtb.AddDef(prd & "_本年", Gettype(Double))
Next
dtb.AddDef("合计_本月", Gettype(Double), sum_y.trim("+"))
dtb.AddDef("合计_本年", Gettype(Double), sum_n.trim("+"))
dtb.AddDef("累计数百分比", Gettype(Double), "合计_本月/合计_本年")
dtb.Build()
For Each cus As String In DataTables("test").GetValues("产品名称")
Dim dr As DataRow = DataTables("统计").AddNew()
dr("产品名称") = cus
For Each prd As String In prds
dr(prd & "_本月") = DataTables("test").Compute("Sum(销售额)","产品名称 = '" & cus & "' And 部门 = '" & prd & "' and 月份 = '" & month & "'")
dr(prd & "_本年") = DataTables("test").Compute("Sum(销售额)","产品名称 = '" & cus & "' And 部门 = '" & prd & "'")
Next
Next
MainTable= Tables("统计")