关于SQLSERVER去掉如何重复值的记录

这个一个在日常工作中所遇到的问题 在此记录一下

dt_user_pay_record表
ID userid time money
1 2 2014-3-2 2
2 2 2015-3-2 33
3 2 2011-9-5 5
4 5 2016-2-2 66
5 5 2015-4-4 77
取出数据
userid time money
2 2015-3-2 33
5 2016-2-2 66
我想取出 userid相同的并且time最大的数据

 

方案1:

select * from (
select id, user_id, created, pay_money,
ROW_NUMBER() over(partition by user_id order by created desc) seq FROM dt_user_pay_record ) tbl where seq =1

 

方案2:

select * from dt_user_pay_record t1
where not exists(select 1 from dt_user_pay_record t2
where t1.user_id=t2.user_id and t2.created>t1.created)

转载于:https://www.cnblogs.com/wt-vip/p/5576814.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值