首页 > 数据库技术 > 详细

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

时间:2016-06-12 12:04:57      阅读:270      评论:0      收藏:0      [点我收藏+]

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

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)

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

原文:http://www.cnblogs.com/wt-vip/p/5576814.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!