Dim dt_tj As DataTable = DataTables("统计")
Dim dt_dd As DataTable = DataTables("订单")
Dim dt_zc As DataTable = DataTables("支出")
dt_tj.DataRows.Clear
Dim minDD As Date = dt_dd.compute("min(日期)")
Dim minZC As Date = dt_zc.compute("min(日期)")
Dim maxDD As Date = dt_dd.compute("max(日期)")
Dim maxZC As Date = dt_zc.compute("max(日期)")
Dim minDate As Date = iif(minDD<minZC, minDD, minZC)
Dim maxDate As Date = iif(maxDD>maxZC, maxDD, maxZC)
For i As Integer = minDate.Year To maxDate.Year
For j As Integer = 1 To 12
Dim ndr As DataRow = dt_tj.AddNew
Dim d As Date = new Date(i, j, 1)
Dim filter As String = "日期 >= #" & d & "# and 日期 < #" & d.AddMonths(1) & "#"
ndr("年份") = i
ndr("月份") = j
ndr("订单_数量") = dt_dd.compute("sum(数量)", filter)
ndr("订单_价格") = dt_dd.compute("sum(价格)", filter)
Dim fdr As DataRow = dt_dd.Find(filter, "日期 desc")
If fdr IsNot Nothing Then
ndr("订单_预留价格") = fdr("预留价格")
Else
ndr("订单_预留价格") = 0
End If
ndr("支出_数量") = dt_zc.compute("sum(次数)", filter)
ndr("支出_单价") = dt_zc.compute("sum(单价)", filter)
fdr = dt_zc.Find(filter, "日期 desc")
If fdr IsNot Nothing Then
ndr("支出_预留单价") = fdr("预留单价")
Else
ndr("支出_预留单价") = 0
End If
Next
Next