select f1,f2,f3 from(
select f1,f2,sum(f3),2 as f4 from table1
union
select '' as f1,f2,sum(f3),3 as f4 from table1
union
select f1,f2,f3,1 as f4 from table1)
order by f4

解决方案 »

  1.   


    --示例--示例数据
    create table table1(f1 char(3),f2 int)
    insert table1 select '001',10
    union  all    select '001',20
    union  all    select '001',30
    union  all    select '002',20
    union  all    select '002',30
    union  all    select '002',40
    union  all    select '003',50
    union  all    select '004',60
    union  all    select '004',70
    go--如果f1+f2不重复,也可以使用
    select f1=case 
    when grouping(f1)=1 then '总计'
    when grouping(f2)=1 then '小计'
    else f1 end,
    f2=sum(f2)
    from table1 
    group by f1,f2 with rollup
    go--删除测试
    drop table table1/*--结果f1   f2          
    ---- ----------- 
    001  10
    001  20
    001  30
    小计   60
    002  20
    002  30
    002  40
    小计   90
    003  50
    小计   50
    004  60
    004  70
    小计   130
    总计   330(所影响的行数为 14 行)
    --*/
      

  2.   

    看清楚我所需要的数据,比一个还要复杂(http://community.csdn.net/Expert/topic/4000/4000094.xml?temp=.3823969)
      

  3.   

    用SQL实现比较困难
    建议搂主采用vsflexgrid 控件的 subtoal 方法添加汇总
    第一次 group on f1,f2 total on f3
    第二次 group on f1 total on f3
    第三次 group on -1 total on f3
      

  4.   

    用with rollup来实现:select * from
    (
    select f1=(case when f2 is null then '总计'
           when grouping(f1)=1 then f2+'小计'
           else  '小计'end)
    ,f2
    ,f3=sum(f3) from table1
    group by f2,f1
    with rollup
    union all
    select * from table1
    ) t
    order by f2,f1
    --结果
    /*
    f1    f2   f3          
    ----- ---- ----------- 
    总计    NULL 400
    001   A    10
    001   A    20
    001   A    30
    002   A    20
    002   A    30
    A小计   A    110
    小计    A    60
    小计    A    50
    002   B    40
    003   B    50
    B小计   B    90
    小计    B    40
    小计    B    50
    004   C    60
    C小计   C    60
    小计    C    60
    004   D    70
    004   D    20
    004   D    30
    005   D    20
    D小计   D    140
    小计    D    120
    小计    D    20(所影响的行数为 24 行)*/
      

  5.   

    --这样好看点;-)select f1,f2,f3 from
    (
    select f1=(case when f2 is null then '总计'
    when grouping(f1)=1 then f2+'小计'
            else  '小计'end)
    ,f2
    ,f3=sum(f3)
    ,[level]=f1 
    from table1

    group by f2,f1
    with rollup
    union all
    select *,[level]=f1 from table1
    ) torder by f2,[level],f1
    --结果
    /*
    f1    f2   f3          
    ----- ---- ----------- 
    总计    NULL 400
    A小计   A    110
    001   A    10
    001   A    20
    001   A    30
    小计    A    60
    002   A    20
    002   A    30
    小计    A    50
    B小计   B    90
    002   B    40
    小计    B    40
    003   B    50
    小计    B    50
    C小计   C    60
    004   C    60
    小计    C    60
    D小计   D    140
    004   D    70
    004   D    20
    004   D    30
    小计    D    120
    005   D    20
    小计    D    20(所影响的行数为 24 行)*/
      

  6.   

    --示例--测试数据
    create table table1(f1 varchar(10),f2 varchar(10),f3 int)
    insert table1 select '001','A',10
    union  all    select '001','A',20
    union  all    select '001','A',30
    union  all    select '002','A',20
    union  all    select '002','A',30
    union  all    select '002','B',40
    union  all    select '003','B',50
    union  all    select '004','C',60
    union  all    select '004','D',70
    union  all    select '004','D',20
    union  all    select '004','D',30 
    union  all    select '005','D',20
    go--查询处理
    select f1=case
    when grouping(f2)=1 then '总计'
    when grouping(f1)=1 then f2+' 合计'
    when grouping(f3)=1 then f1+' 小计'
    else f1 end,
    f2=case when grouping(f3)=0 then f2 else '' end,
    f3=sum(f3)
    from table1
    group by f2,f1,f3 with rollup
    go--删除测试
    drop table table1/*--结果f1              f2         f3          
    --------------- ---------- ----------- 
    001             A          10
    001             A          20
    001             A          30
    001 小计                     60
    002             A          20
    002             A          30
    002 小计                     50
    A 合计                       110
    002             B          40
    002 小计                     40
    003             B          50
    003 小计                     50
    B 合计                       90
    004             C          60
    004 小计                     60
    C 合计                       60
    004             D          20
    004             D          30
    004             D          70
    004 小计                     120
    005             D          20
    005 小计                     20
    D 合计                       140
    总计                         400(所影响的行数为 24 行)
    --*/
      

  7.   

    --示例--测试数据
    create table table1(f1 varchar(10),f2 varchar(10),f3 int)
    insert table1 select '001','A',10
    union  all    select '001','A',20
    union  all    select '001','A',30
    union  all    select '002','A',20
    union  all    select '002','A',30
    union  all    select '002','B',40
    union  all    select '003','B',50
    union  all    select '004','C',60
    union  all    select '004','D',70
    union  all    select '004','D',20
    union  all    select '004','D',30 
    union  all    select '005','D',20
    go--查询处理
    select f1=case
    when grouping(f2)=1 then '总计'
    when grouping(f1)=1 then f2+'小计'
    when grouping(f3)=1 then '小计'
    else f1 end,
    f2=case when grouping(f3)=0 then f2 else '' end,
    f3=sum(f3)
    from table1
    group by f2,f1,f3 with rollup
    go--删除测试
    drop table table1/*--结果f1             f2         f3          
    -------------- ---------- ----------- 
    001            A          10
    001            A          20
    001            A          30
    小计                        60
    002            A          20
    002            A          30
    小计                        50
    A小计                       110
    002            B          40
    小计                        40
    003            B          50
    小计                        50
    B小计                       90
    004            C          60
    小计                        60
    C小计                       60
    004            D          20
    004            D          30
    004            D          70
    小计                        120
    005            D          20
    小计                        20
    D小计                       140
    总计                        400(所影响的行数为 24 行)
    --*/