表TB
日期                名称        数量        金额
2011-10-1        aa          5         10
2011-10-2        aa          10          15
2011-11-20       aa          10         20
2011-11-25       aa          10         30
2012-5-1         aa          5          10
2012-5-20        aa          15         20想得到结果:
日期          数量            金额
2011-10-1    5         10
2011-10-2    10          15
合计           15          25
2011-11-20   10         20
2011-11-25   10         30
合计           20          50
2012-5-1     5          10
2012-5-20   15         20
合计          20          30
也就是按年月进行汇总。谢谢给予帮助的各位们

解决方案 »

  1.   


    create table TB(日期 datetime,名称 varchar(10),数量 int,金额 int)
    insert into tb
    select '2011-10-1',        'aa',          5 ,        10 union all
    select '2011-10-2',        'aa',          10,          15 union all
    select '2011-11-20',       'aa',          10,         20 union all
    select '2011-11-25',       'aa',          10,         30 union all
    select '2012-5-1',         'aa',          5 ,         10 union all
    select '2012-5-20',        'aa',          15,         20
    go
    select case when grouping(日期)= 1 then '合计' 
                else CONVERT(varchar(10),日期,120) end as 日期,
           sum(数量) as 数量,sum(金额) as 金额
    from tb
    group by rollup(CONVERT(varchar(7),日期,120),日期,名称,数量,金额)
    having (GROUPING(日期) =0 and GROUPING(名称) =0 and 
            GROUPING(数量) =0 and GROUPING(金额) =0 and 
            GROUPING(CONVERT(varchar(7),日期,120)) =0) or
           (GROUPING(CONVERT(varchar(7),日期,120))=0 and 
            GROUPING(日期) =1 and GROUPING(名称) =1 and 
            GROUPING(数量) =1 and GROUPING(金额) =1)
    /*
    日期 数量 金额
    2011-10-01 5 10
    2011-10-02 10 15
    合计 15 25
    2011-11-20 10 20
    2011-11-25 10 30
    合计 20 50
    2012-05-01 5 10
    2012-05-20 15 20
    合计 20 30
    */
      

  2.   

    或者这样,用grouping_id函数来简化了一下:
    create table TB(日期 datetime,名称 varchar(10),数量 int,金额 int)
    insert into tb
    select '2011-10-1',        'aa',          5 ,        10 union all
    select '2011-10-2',        'aa',          10,          15 union all
    select '2011-11-20',       'aa',          10,         20 union all
    select '2011-11-25',       'aa',          10,         30 union all
    select '2012-5-1',         'aa',          5 ,         10 union all
    select '2012-5-20',        'aa',          15,         20
    go
    select case when grouping(日期)= 1 then '合计' 
                else CONVERT(varchar(10),日期,120) end as 日期,
           sum(数量) as 数量,sum(金额) as 金额
    from tb
    group by rollup(CONVERT(varchar(7),日期,120),日期,名称,数量,金额)
    having grouping_id(CONVERT(varchar(7),日期,120),日期,名称,数量,金额) =0
           or grouping_id(CONVERT(varchar(7),日期,120),日期,名称,数量,金额) =15/*
    日期 数量 金额
    2011-10-01 5 10
    2011-10-02 10 15
    合计 15 25
    2011-11-20 10 20
    2011-11-25 10 30
    合计 20 50
    2012-05-01 5 10
    2012-05-20 15 20
    合计 20 30
    */
      

  3.   

    我用的是VB6+SQL2000,这语句是在VB中可以用吧。呵呵。
      

  4.   


    改了一下,改成适合2000的了,你再试试:create table TB(日期 datetime,名称 varchar(10),数量 int,金额 int)
    insert into tb
    select '2011-10-1',        'aa',          5 ,        10 union all
    select '2011-10-2',        'aa',          10,          15 union all
    select '2011-11-20',       'aa',          10,         20 union all
    select '2011-11-25',       'aa',          10,         30 union all
    select '2012-5-1',         'aa',          5 ,         10 union all
    select '2012-5-20',        'aa',          15,         20
    go
    select case when grouping(日期)= 1 then '合计' 
                else CONVERT(varchar(10),日期,120) end as 日期,
           sum(数量) as 数量,sum(金额) as 金额
    from tb
    group by CONVERT(varchar(7),日期,120),日期,名称,数量,金额
    with rollup
    having (GROUPING(日期) =0 and GROUPING(名称) =0 and 
            GROUPING(数量) =0 and GROUPING(金额) =0 and 
            GROUPING(CONVERT(varchar(7),日期,120)) =0) or
           (GROUPING(CONVERT(varchar(7),日期,120))=0 and 
            GROUPING(日期) =1 and GROUPING(名称) =1 and 
            GROUPING(数量) =1 and GROUPING(金额) =1)
    /*
    日期 数量 金额
    2011-10-01 5 10
    2011-10-02 10 15
    合计 15 25
    2011-11-20 10 20
    2011-11-25 10 30
    合计 20 50
    2012-05-01 5 10
    2012-05-20 15 20
    合计 20 30
    */
      

  5.   

    以上在SQL2000中测试通过,但在VB程序中不行,不知是咋回事(合计数没有出现)
      

  6.   


    这个代码还是不行吗:create table TB(日期 datetime,名称 varchar(10),数量 int,金额 int)
    insert into tb
    select '2011-10-1',        'aa',          5 ,        10 union all
    select '2011-10-2',        'aa',          10,          15 union all
    select '2011-11-20',       'aa',          10,         20 union all
    select '2011-11-25',       'aa',          10,         30 union all
    select '2012-5-1',         'aa',          5 ,         10 union all
    select '2012-5-20',        'aa',          15,         20
    go
    select case when grouping(日期)= 1 then '合计' 
                else CONVERT(varchar(10),日期,120) end as 日期,
           sum(数量) as 数量,sum(金额) as 金额
    from tb
    group by CONVERT(varchar(7),日期,120),日期,名称,数量,金额
    with rollup
    having (GROUPING(日期) =0 and GROUPING(名称) =0 and 
            GROUPING(数量) =0 and GROUPING(金额) =0 and 
            GROUPING(CONVERT(varchar(7),日期,120)) =0) or
           (GROUPING(CONVERT(varchar(7),日期,120))=0 and 
            GROUPING(日期) =1 and GROUPING(名称) =1 and 
            GROUPING(数量) =1 and GROUPING(金额) =1)
    /*
    日期 数量 金额
    2011-10-01 5 10
    2011-10-02 10 15
    合计 15 25
    2011-11-20 10 20
    2011-11-25 10 30
    合计 20 50
    2012-05-01 5 10
    2012-05-20 15 20
    合计 20 30
    */
      

  7.   


    这样啊,照理不应该呀,看看你的vb程序,是否哪儿有问题呢,你看看vb程序返回了多少条记录,是否拼接语句的时候,拼接错了呢