开发者

Sql get top row for each type

I have a table WorkLog(personId number, actionType number, doneOn timeStamp), now how can i get the most recent action for each person.

the data will be like

p1 a1 timestamp1
p1 a2 timestamp2
p2 a1 timestamp3
p2 a3 timestamp4
p3 a1 timestamp5

the output should be

p1 a2 timestamp2
p2 a1 timestamp3
p3 a1 timestamp5

the开发者_StackOverflow database is DB2


This is a "greatest n per group" query. According to here the below should work in DB2

WITH T AS
(
SELECT *,
       ROW_NUMBER() OVER (PARTITION BY personId ORDER BY doneOn DESC) RN
FROM WorkLog
)
SELECT personId , actionType , doneOn 
FROM T
WHERE RN=1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜