比如我用SQL语句统计查询出一些数据,比如 select mc,sum(sl) as 数量,sum(jie) as 金额,b.xm from a,b where a.id=b.id and a.id=1 group by a.mc,b.xm 得出下面这样结果 mc 和XM我就乱打 反正不重要
mc         数量       金额           XM
aaaa         1         100           b
aaaa         2         200           b
bbbb         2         200           b
aaaa         2         100           b
cccc         1         100           b  如何在SQL里增加语句,直接得出最后有一列mc         数量       金额           XM
aaaa         1         100           b
aaaa         2         200           b
bbbb         2         200           b
aaaa         2         100           b
cccc         1         100           b  
合计         8         700 其实,我也可以每次另存为EXCELG格式,然后最下面求和一下就可以了,但是由于我表太多,每次都要存为EXCEL格式这样求和一下很麻烦,所以问问能不能用SQL语句直接统计增加这么一列

解决方案 »

  1.   

    select yw_ly_id,zy_dx_id from s_business_domain 
    union
    select sum(yw_ly_id),sum(zy_dx_id) from s_business_domain
    好像有个方法可以直接统计的  忘了  你查查吧  哈
      

  2.   


    select  warecode,sum(realqty) from wm_stockmovesub group by rollup(warecode);
      

  3.   

     select mc,sum(sl) as 数量,sum(jie) as 金额,b.xm 
    from a,b where a.id=b.id and a.id=1 group by a.mc,b.xm
    union
    select '合计' as mc,sum(sl) as 数量,sum(jie) as 金额,b.xm 
    from a,b where a.id=b.id and a.id=1 
    group by a.mc
    order by mc
      

  4.   

    如果有多列,而只希望一列累计的话,可以这么做select  warecode,sheetcode,sum(realqty) from wm_stockmovesub group by sheetcode,rollup(warecode);
      

  5.   

    select decode(grouping(mc),1,'合计',mc) as mc,sum(sl) as 数量,sum(jie) as 金额,b.xm from a,b where a.id=b.id and a.id=1 group by rollup(a.mc,b.xm) having grouping(mc)=0 and grouping(xm)=1