sql code

这sql 语句写的真的很不错! 

 

if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col1] bigint,[col2] varchar(6),[col3] varchar(6))
insert [tb]
select 130126200201000275,'王文霞','靳川川' union all
select 130126200201000275,'王文霞','靳澤' union all
select 130126200201000275,'王文霞','aaaa' union all
select 130126200201000276,'周書娥','程思超' union all
select 130126200201000276,'周書娥','程佼佼' union all
select 130126200201000277,'風菊','程青青' union all
select 130126200201000277,'風菊','程二小' union all
select 130126200201000278,'張英桃','程大慶' union all
select 130126200201000278,'張英桃','程小花'
--------------开始查询--------------------------

select * from tb 

 

select col2,col3 from
(
select col1,col2,col3 from [tb] t
where not exists(select * from tb where col1=t.col1 and col2=t.col2 and col3>t.col3)
union all
select col1,'',col3 from [tb] t
where exists(select 1 from tb where col1=t.col1 and col2=t.col2 and col3>t.col3)
)T
order by col1,col3 desc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值