仓库号,产品物料编码,产品物料名称,物料编码(子物料),物料名称,领用数量
001       A               A1                                        100
001  A               A2                                        80
001       A               小计                                      180
001       B               B1                                        20
001       B              小计                                       20
001      合计                                                       200
002      A                A1                                        20
002      A                小计                                      20
002      合计                                                       20
总计                                                                220

解决方案 »

  1.   

    select case when (grouping(d.departname)=1) then '合计'       else isnull(d.departname,'unkown')  end as departname,       sum(s.salaryone) as salaryone, sum(s.salarytwo) as salarytwo from employee e inner join department d on e.departid=d.id 
    inner join  salary s on s.empid=e.id
    group by d.departname with rollup在这上面改装一下.
      

  2.   

    将上面的中文字段与下面的字段名一一对应:select * from
     (select cast(0 as int) as Ord_id,depotno,itemno,item_desc,subitemnno,subitem_desc,out_qty from tabA
      union select 1,depotno,itemno,'小计','','',sum(out_qty) as sum_qty from tabA group by depotno,itemno
      union select 2,depotno,'合计','','','',sum(out_qty) as total_qty from tabA group by depotno
      union select 3,'总计','','','','',sum(out_qty) as total_qty from tabA
     ) a
    order by depotno,ord_id,itemno
      

  3.   

    对了,上面的语名总计的排序可能有误,请改成:
    select * from
     (select 0 as type,cast(0 as int) as Ord_id,depotno,itemno,item_desc,subitemnno,subitem_desc,out_qty from tabA
      union select 0,1,depotno,itemno,'小计','','',sum(out_qty) as sum_qty from tabA group by depotno,itemno
      union select 0,2,depotno,'合计','','','',sum(out_qty) as total_qty from tabA group by depotno
      union select 1,3,'总计','','','','',sum(out_qty) as total_qty from tabA
     ) a
    order by type,depotno,ord_id,itemno  --此处的排序规则不可变以确保总计的记录显示在最后
      

  4.   

    to simonzone(牧羊人):
    怎么改,你说的清楚一些好吗??最好能针对我给
    的表写出SELECT语句
      

  5.   

    不好意思,上面给的数据有误,应该是:仓库号,产品物料编码,产品物料名称,物料编码(子物料),物料名称,领用数量
    001       A                             A1                          100
    001  A                             A2                          80
    001       A                            小计                         180
    001       B                             B1                          20
    001       B                             小计                        20
    001      合计                                                       200
    002      A                              A1                          20
    002      A                             小计                         20
    002      合计                                                       20
    总计                                                                220