重复的字段不显示

数据库:MSSQL
表:
区域    年    月    金额
北京    2005  5     1000
上海    2005  2     2000
北京    2005  3     3000
上海    2005  1     4000
北京    2004  4     3000
 
要求结果:
区域    年    月    金额
北京    2004  4     3000
          2005  3     3000
                   5     1000
上海    2005  1     4000
                   2     2000
---------------------------
 
create table #t(区域 varchar(10),   年 char(4),   月 char(2),   金额 float)
go
insert into #t values('北京',    2005,  5,     1000)
insert into #t values('上海',    2005,  2,     2000)
insert into #t values('北京',    2005,  3,     3000)
insert into #t values('上海',    2005,  1,     4000)
insert into #t values('北京',    2004,  4,     3000)
go
select identity(int,1,1) as id,* into #tt
from #t
order by 区域,年,月
select id,
(case when id=(select top 1 id from #tt where 区域=t.区域 order by 区域) then 区域 else '' end) as 区域,
(case when id=(select top 1 id from #tt where 年=t.年 order by 年) then 年 else '' end) as 年,
月,金额
from #tt t
order by 区域+年+月
注:仍然需要一个唯一id值来区分,才好做。否则,只能:
select
(case when (区域=(select top 1 区域 from #t where 区域=t.区域 order by 区域,年,月) and 年=(select top 1 年 from #t where 区域=t.区域 order by 区域,年,月) and 月=(select top 1 月 from #t where 区域=t.区域 order by 区域,年,月)) then 区域 else '' end) as 区域,
(case when (区域=(select top 1 区域 from #t where 区域=t.区域 order by 区域,年,月) and 年=(select top 1 年 from #t where 区域=t.区域 order by 区域,年,月) and 月=(select top 1 月 from #t where 区域=t.区域 order by 区域,年,月)) then 年 else '' end) as 年,
月,金额
from #t t
order by 区域+年+月
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值