1

I have this table that represents testcase executions, for one particular testcase there could be many executions with different statuses.

id  |  testcase_id  |  status   |  timestamp
-------------------------------------------------------
1   |  2            |  fail     |  2013-01-11 15:00:00
-------------------------------------------------------
2   |  2            |  pass     |  2013-01-11 15:05:00
-------------------------------------------------------
3   |  4            |  fail     |  2013-01-11 16:00:00
-------------------------------------------------------
4   |  4            |  pass     |  2013-01-11 16:04:00

And what I'd like to extract from this table is the latest execution results, ie:

id  |  testcase_id  |  status   |  timestamp
-------------------------------------------------------
2   |  2            |  pass     |  2013-01-11 15:05:00
-------------------------------------------------------
4   |  4            |  pass     |  2013-01-11 16:04:00

How could I achieve this??

4

3 に答える 3

1
SELECT id,testcase_id,status,timestamp
FROM 
    (
        SELECT id,testcase_id,status,timestamp,
                ROW_NUMBER() OVER (PARTITION BY testcase_id
                                    ORDER BY timestamp DESC) rn
        FROM tableName
    ) s
WHERE Rn = 1

また

WITH latestRecord
AS
(
  SELECT id,testcase_id,status,timestamp,
  ROW_NUMBER() OVER (PARTITION BY testcase_id ORDER BY timestamp DESC) rn
  FROM tableName
) 
SELECT id,testcase_id,status,timestamp
FROM latestRecord
WHERE Rn = 1
于 2013-01-11T17:03:57.433 に答える
0

これを試して

select top 2* from TableName where status='pass' order by timestamp asc

出力: http://sqlfiddle.com/#!3/1c810/38/0

于 2013-01-11T17:24:54.743 に答える
0
select tc.*
from testcases tc
inner join (
     select testcase_id,
     max(timestamp) maxTimestamp
     from testcases
     group by testcase_id
     ) tcMaxes
on tc.testcase_id = tcMaxes.testcase_id 
and tc.timestamp = tcMaxes.maxTimestamp
于 2013-01-11T17:06:29.593 に答える