统计各个经营部,各种产品的销售量,used_info是销售表,包括 经营部、时间、项目、人员等字段,
  use_dtl是明细表包括 产品、数量等字段 写了以下语句:
 select a.bu as 经营部,a.pdate  as  时间 ,
     a.item as 项目,   a.rec as 人员 ,
  SUM(decode (b.product_name , '产品1' , b.num ,0)) "产品1",
  SUM(decode(b.product_name , '产品2' , b.num ,0)) "产品2",
  SUM(decode(b.product_name,   '产品3' , b.num ,0))  "产品3", 
 from  used_info a  left join use_dtl b on a.used_id=b.used_id
 group by  a.bu ,a.pdate ,a.item ,a.rec有以下结果:
     经营部     时间      项目  人员  产品1   产品2   产品3
1   A经营部  2006-07-12   项目A  001    1       2        3
2   A经营部 2006-07-12    项目B  001    3        4        5
3   A经营部 2006-07-12    项目B  002    6        0        0
4   B经营部 2006-07-13    项目A  001    2        3        4 
5   B经营部 2006-07-19    项目A  003    10       34       3
6  C经营部 2006-07-29      项目C  003    4       3        6
  
现在想在以上结果最后加上一行
     合计                             26         46       18

     这个合计用rollup语句要怎么写??

解决方案 »

  1.   

    select a.bu as 经营部,a.pdate  as  时间 ,
         a.item as 项目,   a.rec as 人员 ,
      SUM(decode (b.product_name , '产品1' , b.num ,0)) "产品1",
      SUM(decode(b.product_name , '产品2' , b.num ,0)) "产品2",
      SUM(decode(b.product_name,   '产品3' , b.num ,0))  "产品3", 
    from  used_info a  left join use_dtl b on a.used_id=b.used_id
    group by  rollup(a.bu ,a.pdate ,a.item ,a.rec)
    having (grouping(a.pdate)=0 and grouping(a.item)=0 and grouping(a.rec)=0)
           or (grouping(a.bu)=1 and grouping(a.pdate)=1 and grouping(a.item)=1)
      

  2.   

    select decode(grouping_id(a.bu ,a.pdate ,a.item ,a.rec),15,'合计',a.bu) as 经营部,a.pdate  as  时间 ,
         a.item as 项目,   a.rec as 人员 ,
      SUM(decode (b.product_name , '产品1' , b.num ,0)) "产品1",
      SUM(decode(b.product_name , '产品2' , b.num ,0)) "产品2",
      SUM(decode(b.product_name,   '产品3' , b.num ,0))  "产品3", 
    from  used_info a  left join use_dtl b on a.used_id=b.used_id
    group by  rollup(a.bu ,a.pdate ,a.item ,a.rec)
    having grouping_id(a.bu ,a.pdate ,a.item ,a.rec) in (0,15)
      

  3.   

    having grouping_id(....) in (0,15) 是什么意思?