如果存在具有相同TimeStamp_2的行,我需要具有最新TimeStamp_1的行
ID TimeStamp_1 TimeStamp_2
------------------------------------------------
0000003259 2013-05-23 09:53 2013-05-23 09:55
0000003259 2013-05-23 09:52 2013-05-23 09:55
0000003257 2013-05-23 07:52 2013-05-23 07:53
我该怎么做? (上面的表格是JOIN,如果它本身,因此有几个具有相同ID的行)
在上面的示例中,结果集应包含此
ID TimeStamp_1 TimeStamp_2
------------------------------------------------
0000003259 2013-05-23 09:53 2013-05-23 09:55
0000003257 2013-05-23 07:52 2013-05-23 07:53
答案 0 :(得分:2)
根据您提供的数据,最简单的方法似乎是聚合:
select id, min(TimeStamp_1) as TimeStamp_1, TimeStamp_2
from t
group by id, TimeStamp_2;
答案 1 :(得分:1)
请尝试:
SELECT * FROM(
SELECT
ID,
TimeStamp_1,
TimeStamp_2,
ROW_NUMBER() OVER(PARTITION BY ID, TimeStamp_2 ORDER BY TimeStamp_1 DESC) AS RNum
FROM TableName
)x
WHERE RNum=1