--不好意思,把不要贴的贴出来了。
Select case 编号 when 'zzzz' then '' else 编号 end,名称,数量
From (
Select * From Temp
union all
Select left(编号,4),'小计',sum(数量)
from temp group by left(编号,4)
union all
Select 'zzzz', '总计' ,sum(数量)
from temp
) as F
Order By left(编号,4),len(rtrim(编号)) desc,编号
Select * From (
Select * From Temp
union all
Select left(编号,4),'小计',sum(数量)
from temp group by left(编号,4)
union all
Select 'zzzz', '总计' ,sum(数量)
from temp
) as F
Order By left(编号,4),len(rtrim(编号)) desc,编号
case substring(id,5,2) when ' ' then '99' else id)
呵呵...
Select case id when 9999 then '' else id end
From (
Select * From Table1
union all
Select SubString(ID, 1, 4) as ID,
'小计' as Name, sum(qty) as qty
from table1
Group By SubString(ID, 1, 4)
union all
Select '9999' as ID, '总计' as Name, sum(qty) as qty
from table1
) as F
Order By SubString(ID, 1, 4),
case substring(id,5,2) when ' ' then '99' else id)
create table #temp (
id int identity (1,1),
bh char (10),
name char (10),
books int
)
go
insert #temp valuse (bh,name,books) select 字段+编号,名称,数量 from temp where 字段='0001' order by 编号
go
insert #temp valuse (bh,name,books) select 字段,‘小计’,sum(数量) from temp group by 字段 where 字段='0001'
go
insert #temp valuse (bh,name,books) select 字段+编号,名称,数量 from temp where 字段='0002' order by 编号
go
insert #temp valuse (bh,name,books) select 字段,‘小计’,sum(数量) from temp group by 字段 where 字段='0002'
go
insert #temp valuse (bh,name,books) select ‘’,‘合计’,sum(数量) from temp
go
select bh,name,books from #temp
go
若记录比较多,可以采用循环处理。
Select * From (
Select * From Table1
union all
Select SubString(ID, 1, 4) as ID, '小计' as Name, sum(qty) as qty
from table1
Group By SubString(ID, 1, 4)
union all
Select '9999' as ID, '总计' as Name, sum(qty) as qty
from table1
) as F
Order By SubString(ID, 1, 4)