可以用case when
group by  
with cube
之類的來實現

解决方案 »

  1.   

    试试:
    select a.*,b.* from 
    (select no,min(invno) as ino,typ1=sum(case when type='1' then score else 0 end),
    typ2=sum(case when type='2' then score else 0 end),'表A' as where_from
    from qqa1 group by no union
    select case when grouping(no)=1 then 'hj' else no end ,rtrim(no)+'小计' as ino,sum(typ1) as typ1 ,sum(typ2) as typ2,' ' as where_from
    from (select no,min(invno) as ino,typ1=sum(case when type='1' then score else 0 end),typ2=sum(case when type='2' then score else 0 end)
    from qqa1 group by no) dd group by no with rollup ) a left join qqc1 b on b.no=a.no and a.where_from<>' ' order by a.no,a.ino 
      

  2.   

    精简一些:
    select case when substring(ida,2,2)='合计' then ' ' else where_from end as where_from,
    * from 
    (select isnull(id,'总计') as ida,* from 
    (select a.*,b.* from 
    (select case when grouping(invno)='1' then rtrim(no)+'合计' 
    else no end as id,
    min(invno) as ino,typ1=sum(case when type='1' then score else 0 end),
    typ2=sum(case when type='2' then score else 0 end),'表A' as where_from
    from qqa1 group by no,invno with rollup) a left join qqc1 b on b.no=a.id 
     )c)d
      

  3.   

    用pb做好了,别把sql 搞那么复杂了