Foxtable(狐表)用户栏目专家坐堂 → 不加载表的情况下组合统计


  共有3331人关注过本帖树形打印复制链接

主题:不加载表的情况下组合统计

帅哥哟,离线,有人找我吗?
有点甜
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/6/30 15:46:00 [显示全部帖子]

以下是引用zhangxl1964在2014-6-30 14:55:00的发言:
编写SQL语句生成表?能否给点提示?

 

http://www.foxtable.com/help/topics/2314.htm

 

http://www.foxtable.com/help/topics/2320.htm

 

http://www.foxtable.com/help/topics/2307.htm

 

 


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/7/1 9:06:00 [显示全部帖子]

 你说的多对多是不是这个意思?

 

http://www.foxtable.com/help/topics/2320.htm

 

 

五、UNION 运算符

UNION运算符用于组合两个查询的结果。
例如有一个客户表,一个供应商表,我需要得到所有在中国的客户和供应商的名称和地址。
因为数据位于不同的表中,显然,我们需要用两个查询才能完成任务:

SELECT 公司名称, 地址 FROM {客户} WHERE 国家 = '中国' UNION SELECT 公司名称, 地址 FROM {供应商} WHERE 国家 = '中国'

两个查询的字段名称、个数、类型必须完全一致才行。

默认情况下,UNION会自动排除重复的行,然后你可以用ALL关键词来确保返回所有的行,例如:

SELECT 公司名称, 地址 FROM {客户} WHERE 国家 = '中国' UNION ALL SELECT 公司名称, 地址 FROM {供应商} WHERE 国家 = '中国'


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/7/1 9:29:00 [显示全部帖子]

代码

 

If DataTables.Contains("进货") = False Then
    DataTables.Load("进货|销售|退货")
End If

With e.Form.Controls("日期")
    If .Value Is Nothing Then
        Return
    End If
End With
Tables("统计_Table1").StopRedraw
Dim Filter As String
Dim curD As Date=e.Form.Controls("日期").Value '当前日期
Dim lastD As Date = curD.AddMonths(-1) '上一个月的日期
Dim lastD2 As New Date(lastD.Year, lastD.Month, Date.DaysInMonth(lastD.Year,lastD.Month)) '上个月的最后一天
Filter = Filter &  "日期 > #" & lastD2 & "#" & " And 日期  <= #" & curD & "#"

Dim bd1 As New SQLGroupTableBuilder("统计表1","进货")
Dim dt1 As fxDataSource
bd1.Groups.AddDef("型号") '根据型号分组
bd1.Totals.AddDef("进货_数量","进货_数量") '对数量进行统计
bd1.Totals.AddDef("进货_金额","进货_金额") '对金额进行统计
bd1.Filter = Filter
dt1 = bd1.BuildDataSource()

Dim bd2 As New SQLGroupTableBuilder("统计表2","销售")
Dim dt2  As fxDataSource
bd2.Groups.AddDef("型号") '根据型号分组
bd2.Totals.AddDef("销售_数量","销售_数量") '对数量进行统计
bd2.Totals.AddDef("销售_金额","销售_金额") '对金额进行统计
bd2.Filter = Filter
dt2 = bd2.BuildDataSource()

Dim bd3 As New SQLGroupTableBuilder("统计表3","退货")
Dim dt3 As fxDataSource
bd3.Groups.AddDef("型号") '根据型号分组
bd3.Totals.AddDef("退货_数量","退货_数量") '对数量进行统计
bd3.Totals.AddDef("退货_金额","退货_金额") '对金额进行统计
bd3.Filter = Filter
dt3 = bd3.BuildDataSource()

dt1.Combine("型号",dt2,"型号") '将销售统计数据组合到进货统计数据
dt1.Combine("型号",dt3,"型号") '将退货统计数据组合到进货统计数据
Tables("统计_Table1").DataSource = dt1 '将统计结果绑定到Table

With DataTables("统计_Table1").DataCols  '用表达式列计算库存数据
    .Add("期初_数量",Gettype(Double),"","期初_数量")
    .Add("期初_金额",Gettype(Double),"","期初_金额")
End With

Dim dt_jh As DataTable
Dim dt_xs As DataTable
Dim dt_th As DataTable
Dim cmd As new SQLCommand
cmd.CommandText = "select * from {进货} where " & Filter
dt_jh = cmd.ExecuteReader
cmd.CommandText = "select * from {销售} where " & Filter
dt_xs = cmd.ExecuteReader
cmd.CommandText = "select * from {退货} where " & Filter
dt_th = cmd.ExecuteReader

For Each r As Row In Tables("统计_Table1").Rows
    Filter = Filter & " and 型号 = '" & r("型号") & "'"
    Filter = "日期 <=  #" & lastD2 & "# and 型号 = '" & r("型号") & "'"
    r("期初_数量") = dt_jh.Compute("sum(进货_数量)", filter) - dt_xs.Compute("sum(销售_数量)", filter) - dt_th.Compute("sum(退货_数量)", filter)
    r("期初_金额") = dt_jh.Compute("sum(进货_金额)", filter) - dt_xs.Compute("sum(销售_金额)", filter) - dt_th.Compute("sum(退货_金额)", filter)
Next
With DataTables("统计_Table1").DataCols  '用表达式列计算库存数据
    .Add("库存_数量",Gettype(Double), "IsNull([期初_数量],0) + IsNull([进货_数量],0) - ISNULL([销售_数量],0) - ISNULL([退货_数量],0)","库存_数量")
    .Add("库存_金额",Gettype(Double), "IsNull([期初_金额],0) + IsNull([进货_金额],0) - ISNULL([销售_金额],0) - ISNULL([退货_金额],0)","库存_金额")
End With
Tables("统计_Table1").SetColVisibleWidth("型号|80|期初_数量|80|期初_金额|80|进货_数量|80|进货_金额|80|销售_数量|80|销售_金额|80|退货_数量|80|退货_金额|80|库存_数量|80|库存_金额|80")

Tables("统计_Table1").DefaultRowHeight = 35
DataTables("统计_Table1").DataCols("期初_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("期初_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("进货_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("进货_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("销售_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("销售_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("退货_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("退货_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("库存_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("库存_金额").SetFormat("#,###.00")
Tables("统计_Table1").ResumeRedraw


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/7/1 10:23:00 [显示全部帖子]

以下是引用zhangxl1964在2014-7-1 10:17:00的发言:
这个统计方式,对于很大的数据量是否不太合适?有否其他的针对大数据量的加载临时表方式?

 

大数据也是这样统计的。


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/7/1 11:03:00 [显示全部帖子]

If DataTables.Contains("进货") = False Then
    DataTables.Load("进货|销售|退货")
End If

With e.Form.Controls("日期")
    If .Value Is Nothing Then
        Return
    End If
End With
Tables("统计_Table1").StopRedraw
Dim Filter As String
Dim curD As Date=e.Form.Controls("日期").Value '当前日期
Dim lastD As Date = curD.AddMonths(-1) '上一个月的日期
Dim lastD2 As New Date(lastD.Year, lastD.Month, Date.DaysInMonth(lastD.Year,lastD.Month)) '上个月的最后一天
Filter = Filter &  "日期 > #" & lastD2 & "#" & " And 日期  <= #" & curD & "#"

Dim bd1 As New SQLGroupTableBuilder("统计表1","进货")
Dim dt1 As fxDataSource
bd1.Groups.AddDef("型号") '根据型号分组
bd1.Totals.AddDef("进货_数量","进货_数量") '对数量进行统计
bd1.Totals.AddDef("进货_金额","进货_金额") '对金额进行统计
bd1.Filter = Filter
dt1 = bd1.BuildDataSource()

Dim bd2 As New SQLGroupTableBuilder("统计表2","销售")
Dim dt2  As fxDataSource
bd2.Groups.AddDef("型号") '根据型号分组
bd2.Totals.AddDef("销售_数量","销售_数量") '对数量进行统计
bd2.Totals.AddDef("销售_金额","销售_金额") '对金额进行统计
bd2.Filter = Filter
dt2 = bd2.BuildDataSource()

Dim bd3 As New SQLGroupTableBuilder("统计表3","退货")
Dim dt3 As fxDataSource
bd3.Groups.AddDef("型号") '根据型号分组
bd3.Totals.AddDef("退货_数量","退货_数量") '对数量进行统计
bd3.Totals.AddDef("退货_金额","退货_金额") '对金额进行统计
bd3.Filter = Filter
dt3 = bd3.BuildDataSource()

dt1.Combine("型号",dt2,"型号") '将销售统计数据组合到进货统计数据
dt1.Combine("型号",dt3,"型号") '将退货统计数据组合到进货统计数据
Tables("统计_Table1").DataSource = dt1 '将统计结果绑定到Table

With DataTables("统计_Table1").DataCols  '用表达式列计算库存数据
    .Add("期初_数量",Gettype(Double),"","期初_数量")
    .Add("期初_金额",Gettype(Double),"","期初_金额")
End With

Filter = "日期 <=  #" & lastD2 & "#"
Dim dt_jh As DataTable
Dim dt_xs As DataTable
Dim dt_th As DataTable
Dim cmd As new SQLCommand
cmd.CommandText = "select * from {进货} where " & Filter
dt_jh = cmd.ExecuteReader
cmd.CommandText = "select * from {销售} where " & Filter
dt_xs = cmd.ExecuteReader
cmd.CommandText = "select * from {退货} where " & Filter
dt_th = cmd.ExecuteReader

For Each r As Row In Tables("统计_Table1").Rows   
    Filter = "型号 = '" & r("型号") & "'"
    r("期初_数量") = dt_jh.Compute("sum(进货_数量)", filter) - dt_xs.Compute("sum(销售_数量)", filter) - dt_th.Compute("sum(退货_数量)", filter)
    r("期初_金额") = dt_jh.Compute("sum(进货_金额)", filter) - dt_xs.Compute("sum(销售_金额)", filter) - dt_th.Compute("sum(退货_金额)", filter)
Next
With DataTables("统计_Table1").DataCols  '用表达式列计算库存数据
    .Add("库存_数量",Gettype(Double), "IsNull([期初_数量],0) + IsNull([进货_数量],0) - ISNULL([销售_数量],0) - ISNULL([退货_数量],0)","库存_数量")
    .Add("库存_金额",Gettype(Double), "IsNull([期初_金额],0) + IsNull([进货_金额],0) - ISNULL([销售_金额],0) - ISNULL([退货_金额],0)","库存_金额")
End With
Tables("统计_Table1").SetColVisibleWidth("型号|80|期初_数量|80|期初_金额|80|进货_数量|80|进货_金额|80|销售_数量|80|销售_金额|80|退货_数量|80|退货_金额|80|库存_数量|80|库存_金额|80")

Tables("统计_Table1").DefaultRowHeight = 35
DataTables("统计_Table1").DataCols("期初_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("期初_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("进货_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("进货_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("销售_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("销售_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("退货_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("退货_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("库存_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("库存_金额").SetFormat("#,###.00")
Tables("统计_Table1").ResumeRedraw


 回到顶部