以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [分享]利用SQLGroupTableBuilder作年龄段分组统计。  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=31504)

--  作者:pc005637
--  发布时间:2013/4/16 16:47:00
--  [分享]利用SQLGroupTableBuilder作年龄段分组统计。

数据源:MSSQL2000

外部表名:"xxx"

统计用到的列:"年龄"\\"锁定"\\"审核状态"

 

 "年龄分组"
        Dim old As String
        Dim n As Integer = 5 \'年龄段划分的区间值
        Dim p As Integer = 20 \'年龄段的区间数目
        For i As Integer = 0 To p
            Dim d As String = Format(i * n,"000") & "-" & Format((i + 1) * n - 1,"000")
            If i = 0 Then
                old = "(Case When 年龄 < " & (i + 1) * n & " Then \'" & d & "\'"
            ElseIf i = p Then
                old = old & " When 年龄 < " & (i + 1) * n & " Then \'" & d & "\' Else \'大于" & (i + 1) * n & "\' End)"
            Else
                old = old & " When 年龄 < " & (i + 1) * n & " Then \'" & d & "\'"
            End If
        Next
       
        Dim nl1 As New SQLGroupTableBuilder("统计表1","xxx")
        nl1.C
        nl1.Groups.AddExp("年龄段", old)
        nl1.Totals.AddDef("锁定", AggregateEnum.Count, "未审核")
        nl1.Filter = "审核状态 = \'未审核\'"
        Dim dsnl1 As fxDataSource
        dsnl1 = nl1.BuildDataSource()
       
        Dim nl2 As New SQLGroupTableBuilder("统计表2","xxx")
        nl2.C
        nl2.Groups.AddExp("年龄段", old)
        nl2.Totals.AddDef("锁定", AggregateEnum.Count, "已审核")
        nl2.Filter = "审核状态 = \'已审核\'"
        nl2.GrandProportion = True
        Dim dsnl2 As fxDataSource
        dsnl2 = nl2.BuildDataSource()
       
        dsnl1.Combine("年龄段",dsnl2,"年龄段")
        Dim t_nl As Table = Tables("窗口A_Table1")
        t_nl.DataSource = dsnl1
        Dim g_nl As Subtotalgroup
        t_nl.SubtotalGroups.Clear()
        g_nl = New Subtotalgroup \'定义一个新的分组
        g_nl.Aggregate = AggregateEnum.Sum \'统计类型为求和
        g_nl.GroupOn = "*" \'分组列
        g_nl.TotalOn = "未审核,已审核" \'设置统计列
        g_nl.Caption = "小计" \'设置标题
        t_nl.Sort = "年龄段"
        t_nl.SubtotalGroups.Add(g_nl) \'加到分组集合中
        t_nl.Subtotal() \'生成汇总模式
        t_nl.AutoSizeCols
        Forms("窗口A").Controls("Table1").Visible = True
        DataTables("窗口A_Table1").SysStyles("Alternate").BackColor = Color.PowderBlue


--  作者:pc005637
--  发布时间:2013/4/16 16:49:00
--  [注意]如果数据表是内部表。
应该用IIF() 函数作区分。