create table test
(
订单号 varchar(7),
品名 varchar(5),
金额 int
)insert into test select 'P061001',     'End1',    180
insert into test select 'P061001',     'END2',    260
insert into test select 'P061001',     'END3',    330
insert into test select 'P061200',     'PON1',    150
insert into test select 'P061200',     'PON2',    510
insert into test select 'P061200',     'PON3',    150select case when GROUPING(订单号) = 1 then '总计' when 品名 is null then '小计' else 订单号 end as 订单号,
isnull(品名,'') as 品名,sum(金额) as 金额 
from test
group by 订单号,品名 with ROLLUP P061001 End1 180
P061001 END2 260
P061001 END3 330
小计 770
P061200 PON1 150
P061200 PON2 510
P061200 PON3 150
小计 810
总计 1580

解决方案 »

  1.   

    select * from tablename where 订单号='P061001'
    union all
    select 小计 as 订单号,品名='',金额=sum(金额) from tablename where 订单号='P061001'
    union all
    select * from tablename where 订单号='P061002'
    union all
    select 小计 as 订单号,品名='',金额=sum(金额) from tablename where 订单号='P061002'
    union all
    select 总计 as 订单号,品名='',金额=sum(金额) from tablename 
      

  2.   

    select ddh,pm,je from test_pp
    union 
    select ddh+'小计','',sum(je) from test_pp
    group by ddh
    union 
    select '总计','',sum(je) from test_pp