tb_AA
 Id        counts
 a001        80
 a002        80
 a003        80
 a002        80
 a003        80
 a003        80
 a002        80
 a002        80
 a002        80
 a001        80得到的样式如下;
 先是按id 分类汇总 后,再统一求和
tb_AA
  ID           counts
  a001          160
  a002          400
  a003          240
  合计          800能不能用一条sql语句显示出来?

解决方案 »

  1.   

    select isnull(rtrim(Id),'合计')as Id,sum(counts) as counts
    from tb_AA
    group by Id,with rollup
      

  2.   

    if object_id('[tb_AA]') is not null drop table [tb_AA]
    go
    create table [tb_AA]([Id] varchar(4),[counts] int)
    insert [tb_AA]
    select 'a001',80 union all
    select 'a002',80 union all
    select 'a003',80 union all
    select 'a002',80 union all
    select 'a003',80 union all
    select 'a003',80 union all
    select 'a002',80 union all
    select 'a002',80 union all
    select 'a002',80 union all
    select 'a001',80
    go
    --select * from [tb_AA]select id=isnull(id,'合计'),counts=sum(counts)
    from tb_AA
    group by id with rollup
    /*
    id   counts
    ---- -----------
    a001 160
    a002 400
    a003 240
    合计   800(4 行受影响)
    */
      

  3.   


    select isnull(rtrim(Id),'合计') as Id,sum(counts) as counts
    from tb_AA
    group by Id with rollup
      

  4.   

    select id,sum(counts)from tb_AA
    GROUP by id
      

  5.   

    select id,sum(counts) as counts from tb_aa group by id
    union all 
    select '合计' as id,sum(counts) as counts from tb_aa
      

  6.   

    select id,sum(counts) as counts from tb_AA group by id
    union
    select '合计' as id ,(select sum(counts) as counts from tb_AA) as counts
      

  7.   

    with rollup 是干什么的? 高手解释一下
      

  8.   


    if object_id('[tb_AA]') is not null drop table [tb_AA]
    go
    create table [tb_AA]([Id] varchar(4),[counts] int)
    insert [tb_AA]
    select 'a001',80 union all
    select 'a002',80 union all
    select 'a003',80 union all
    select 'a002',80 union all
    select 'a003',80 union all
    select 'a003',80 union all
    select 'a002',80 union all
    select 'a002',80 union all
    select 'a002',80 union all
    select 'a001',80
    goselect ID=ISNULL(Id,'合计'),SUM(counts)as counts from tb_AA group by Id with rolluprollup是做汇总来用的
      

  9.   

    select id=isnull(id,'合计'),counts=sum(counts)
    from tb_AA
    group by id with rollup
    用 ROLLUP 汇总数据
    在生成包含小计和合计的报表时,ROLLUP 运算符很有用。ROLLUP 运算符生成的结果集类似于 CUBE 运算符所生成的结果集。有关更多信息.CUBE 和 ROLLUP 之间的区别在于: CUBE 生成的结果集显示了所选列中值的所有组合的聚合。
    ROLLUP 生成的结果集显示了所选列中值的某一层次结构的聚合。 
    例如,简单表 Inventory 中包含: 
    Item                 Color                Quantity                   
    -------------------- -------------------- -------------------------- 
    Table                Blue                 124                        
    Table                Red                  223                        
    Chair                Blue                 101                        
    Chair                Red                  210                        
    下列查询将生成小计报表:
    SELECT CASE WHEN (GROUPING(Item) = 1) THEN 'ALL'
                ELSE ISNULL(Item, 'UNKNOWN')
           END AS Item,
           CASE WHEN (GROUPING(Color) = 1) THEN 'ALL'
                ELSE ISNULL(Color, 'UNKNOWN')
           END AS Color,
           SUM(Quantity) AS QtySum
    FROM Inventory
    GROUP BY Item, Color WITH ROLLUPItem                 Color                QtySum                     
    -------------------- -------------------- -------------------------- 
    Chair                Blue                 101.00                     
    Chair                Red                  210.00                     
    Chair                ALL                  311.00                     
    Table                Blue                 124.00                     
    Table                Red                  223.00                     
    Table                ALL                  347.00                     
    ALL                  ALL                  658.00                     (7 row(s) affected)
    如果查询中的 ROLLUP 关键字更改为 CUBE,那么 CUBE 结果集与上述结果相同,只是在结果集的末尾还会返回下列两行:
    ALL                  Blue                 225.00                     
    ALL                  Red                  433.00                     
    CUBE 操作为 Item 和 Color 中值的可能组合生成行。例如,CUBE 不仅报告与 Item 值 Chair 相组合的 Color 值的所有可能组合(Red、Blue 和 Red + Blue),而且报告与 Color 值 Red 相组合的 Item 值的所有可能组合(Chair、Table 和 Chair + Table)。 对于 GROUP BY 子句中右边的列中的每个值,ROLLUP 操作并不报告左边一列(或左边各列)中值的所有可能组合。例如,ROLLUP 并不对每个 Color 值报告 Item 值的所有可能组合。 ROLLUP 操作的结果集具有类似于 COMPUTE BY 所返回结果集的功能;然而,ROLLUP 具有下列优点: ROLLUP 返回单个结果集;COMPUTE BY 返回多个结果集,而多个结果集会增加应用程序代码的复杂性。
    ROLLUP 可以在服务器游标中使用;COMPUTE BY 不可以。
    有时,查询优化器为 ROLLUP 生成的执行计划比为 COMPUTE BY 生成的更为高效。 
      

  10.   

    select isnull(rtrim(Id),'合计')as Id,sum(counts) as counts
    from tb_AA
    group by Id,with rollup
      

  11.   

    declare @tb_AA table([Id] varchar(10),[counts] int)
    insert into @tb_AA([Id],[counts])
    select 'a001',80 union all
    select 'a002',80 union all
    select 'a003',80 union all
    select 'a002',80 union all
    select 'a003',80 union all
    select 'a003',80 union all
    select 'a002',80 union all
    select 'a002',80 union all
    select 'a002',80 union all
    select 'a001',80select [Id],sum([counts]) as [counts] from @tb_AA group by [Id]
    union
    select '合计',sum([counts]) from @tb_AA Id counts
    a001 160
    a002 400
    a003 240
    合计 800