我在SQLite中有一个表:
CREATE TABLE test_results(timestamp TEXT, npass INTEGER, nfails INTEGER)
我想返回每天的最后一次通过/失败信息。例如,如果表包含...
2009-08-31 23:30:19|0|24 2009-08-31 23:37:18|0|24 2009-08-31 23:40:00|0|24 2009-09-01 19:02:13|0|2 2009-09-01 19:08:24|2|0 2009-09-01 19:20:29|2|0
我想要一个select语句来返回......
2009-08-31 23:40:00|0|24 2009-09-01 19:20:29|2|0
提前致谢!
答案 0 :(得分:8)
尝试:
SELECT t.timestamp,
t.npass,
t.nfails
FROM TEST_RESULTS t
JOIN (SELECT MAX(tt.timestamp) 'maxtimestamp'
FROM TEST_RESULTS tt
GROUP BY date(tt.timestamp)) m ON m.maxtimestamp = t.timestamp
答案 1 :(得分:1)
您可以尝试此查询:
SELECT * FROM (select * from test_results ORDER BY timestamp)
GROUP BY date(timestamp)
如果您还希望每天都能获得第一次失败:
SELECT * FROM (select * from test_results ORDER BY timestamp DESC)
GROUP BY date(timestamp)
答案 2 :(得分:0)
这就是我解决这类问题的方法:
SELECT t1.*
FROM test_results t1
LEFT OUTER JOIN test_results t2
ON (DATE(t1.timestamp) = DATE(t2.timestamp) AND t1.timestamp < t2.timestamp)
WHERE t2.timestamp IS NULL;
这假定timestamp
列对其具有唯一或主键约束。否则,每天可以获得超过一行的(小)机会。