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
精彩评论