嘿嘿,没注意,日期重复不能合并,那么用这个:
select case when 地区 is null then '合计'
when id is null then '小计' else 地区 end 地区,
sum([1号]) [1号],sum([2号]) [2号],sum([3号]) [3号],sum(业绩) 业绩
from (select 地区,newid() id,
case when 日期='2006.01.01' then 收入 else 0 end [1号],
case when 日期='2006.01.02' then 收入 else 0 end [2号],
case when 日期='2006.01.03' then 收入 else 0 end [3号],
收入 [业绩]
from 原始表
)T group by 地区,ID with rollup
带排序的:
select 地区,[1号],[2号],[3号],业绩
from (select case when 地区 is null then 1 else 0 end ord1,
地区 ord2,
case when id is null then 1 else 0 end ord3,
case when 地区 is null then '合计'
when id is null then '小计' else 地区 end 地区,
sum([1号]) [1号],sum([2号]) [2号],sum([3号]) [3号],sum(业绩) 业绩
from (select 地区,newid() id,
case when 日期='2006.01.01' then 收入 else 0 end [1号],
case when 日期='2006.01.02' then 收入 else 0 end [2号],
case when 日期='2006.01.03' then 收入 else 0 end [3号],
收入 [业绩]
from 原始表
)T1 group by 地区,ID with rollup
)T2
order by ord1,ord2,ord3