GROUPING
是一个聚合函数,它产生一个附加的列,当用 CUBE 或 ROLLUP 运算符添加行时,附加的列输出值为1,当所添加的行不是由 CUBE 或 ROLLUP 产生时,附加列值为0。仅在与包含 CUBE 或 ROLLUP 运算符的 GROUP BY 子句相联系的选择列表中才允许分组。语法
GROUPING ( column_name )参数
column_name是 GROUP BY 子句中用于检查 CUBE 或 ROLLUP 空值的列。返回类型
int注释

解决方案 »

  1.   

    create table ta(部门名称 varchar(4),物品名称 varchar(10),数量 int,单价 int,年度 int)
    insert ta select 
    'A','PC',  5   , 6000,  2006 union select
    'A','Printer',  1 ,   2500,  2006 union select
    'A','Scanner',  2,    2500 , 2006 union select
    'B','PC',  8   , 6000 , 2006 union select
    'B','Printer',  2,    2500,  2006 union select
    'C','DC',  1 ,   3000,  2006 union select
    'C','PC',  2,    6000 , 2006 
    goselect isnull( 部门名称,'合计') 部门名称 ,
    isnull(物品名称,'合计') ,
    case when  grouping(部门名称)= 0  and grouping(物品名称)= 0 then ltrim(max(数量)) else '' end as 数量,
    case when  grouping(部门名称)= 0  and grouping(物品名称)= 0 then ltrim(max(单价)) else '' end as 单价 ,
    总额 = max(数量 *单价),
    max(年度) 年度
    from ta
    group by 
     部门名称 ,
    物品名称 with cube
    order by grouping(部门名称) desc,部门名称,grouping(物品名称) desc
    /*
    部门名称            数量           单价           总额          年度          
    ---- ---------- ------------ ------------ ----------- ----------- 
    合计   合计                                   48000       2006
    合计   DC                                   3000        2006
    合计   PC                                   48000       2006
    合计   Printer                              5000        2006
    合计   Scanner                              5000        2006
    A    合计                                   30000       2006
    A    Scanner    2            2500         5000        2006
    A    PC         5            6000         30000       2006
    A    Printer    1            2500         2500        2006
    B    合计                                   48000       2006
    B    PC         8            6000         48000       2006
    B    Printer    2            2500         5000        2006
    C    合计                                   12000       2006
    C    PC         2            6000         12000       2006
    C    DC         1            3000         3000        2006(所影响的行数为 15 行)
    */
    drop table ta
      

  2.   

    create table ta(部门名称 varchar(4),物品名称 varchar(10),数量 int,单价 int,年度 int)
    insert ta select 
    'A','PC',  5   , 6000,  2006 union select
    'A','Printer',  1 ,   2500,  2006 union select
    'A','Scanner',  2,    2500 , 2006 union select
    'B','PC',  8   , 6000 , 2006 union select
    'B','Printer',  2,    2500,  2006 union select
    'C','DC',  1 ,   3000,  2006 union select
    'C','PC',  2,    6000 , 2006 
    goselect 
    case when  grouping(部门名称)= 1 and grouping(物品名称)= 1 then '合计' else isnull(部门名称,'') end  部门名称,
    isnull(物品名称,'合计') ,
    case when  grouping(部门名称)= 0  and grouping(物品名称)= 0 then ltrim(max(数量)) else '' end as 数量,
    case when  grouping(部门名称)= 0  and grouping(物品名称)= 0 then ltrim(max(单价)) else '' end as 单价 ,
    总额 = max(数量 *单价),
    max(年度) 年度
    from ta
    group by 
     部门名称 ,
    物品名称 with cube
    order by grouping(部门名称) desc,
             case 部门名称 when  '' then '1' when '合计' then '0' else 部门名称 end ,
             grouping(物品名称) desc
    /*部门名称            数量           单价           总额          年度          
    ---- ---------- ------------ ------------ ----------- ----------- 
    合计   合计                                   48000       2006
         DC                                   3000        2006
         PC                                   48000       2006
         Printer                              5000        2006
         Scanner                              5000        2006
    A    合计                                   30000       2006
    A    Scanner    2            2500         5000        2006
    A    PC         5            6000         30000       2006
    A    Printer    1            2500         2500        2006
    B    合计                                   48000       2006
    B    PC         8            6000         48000       2006
    B    Printer    2            2500         5000        2006
    C    合计                                   12000       2006
    C    PC         2            6000         12000       2006
    C    DC         1            3000         3000        2006(所影响的行数为 15 行)*/
    drop table ta
      

  3.   

    create table ta(部门名称 varchar(4),物品名称 varchar(10),数量 int,单价 int,年度 int)
    insert ta select 
    'A','PC',  5   , 6000,  2006 union select
    'A','Printer',  1 ,   2500,  2006 union select
    'A','Scanner',  2,    2500 , 2006 union select
    'B','PC',  8   , 6000 , 2006 union select
    'B','Printer',  2,    2500,  2006 union select
    'C','DC',  1 ,   3000,  2006 union select
    'C','PC',  2,    6000 , 2006 
    goselect 
    case when  grouping(部门名称)= 1 and grouping(物品名称)= 1 then '合计' else isnull(部门名称,'') end  部门名称,
    isnull(物品名称,'合计') 物品名称 ,
    case when  grouping(部门名称)= 0  and grouping(物品名称)= 0 then ltrim(max(数量)) else '' end as 数量,
    case when  grouping(部门名称)= 0  and grouping(物品名称)= 0 then ltrim(max(单价)) else '' end as 单价 ,
    总额 = max(数量 *单价),
    max(年度) 年度
    from ta
    group by 
     部门名称 ,
    物品名称 with cube
    order by grouping(部门名称) desc,
             case 部门名称 when  '' then '1' when '合计' then '0' else 部门名称 end ,
             grouping(物品名称) desc
    /*部门名称 物品名称       数量           单价           总额          年度          
    ---- ---------- ------------ ------------ ----------- ----------- 
    合计   合计                                   48000       2006
         DC                                   3000        2006
         PC                                   48000       2006
         Printer                              5000        2006
         Scanner                              5000        2006
    A    合计                                   30000       2006
    A    Scanner    2            2500         5000        2006
    A    PC         5            6000         30000       2006
    A    Printer    1            2500         2500        2006
    B    合计                                   48000       2006
    B    PC         8            6000         48000       2006
    B    Printer    2            2500         5000        2006
    C    合计                                   12000       2006
    C    PC         2            6000         12000       2006
    C    DC         1            3000         3000        2006(所影响的行数为 15 行)*/
    drop table ta