我有下表:
Test_Results
|ID|Test Name| Status| Retry|
|1 | test1 | passed| 0 |
|2 | test2 | failed| 0 |
|3 | test2 | passed| 1 |
|4 | test3 | failed| 0 |
|5 | test3 | failed| 1 |
|6 | test4 | failed| 0 |
|7 | test4 | failed| 1 |
|8 | test4 | passed| 2 |
逻辑如下:如果测试失败,则重试,重试字段指示重试次数。例如,测试2失败(ID 2)并在重试(ID 3)后成功。或者,test4失败2次(ID为6和7)并且在第8次成功。
我需要一个查询,它将选择仅采用最后重试结果的所有测试。因此,在test2的情况下,它将计算ID3 =传递,对于test3,它将花费ID 5 =失败,而对于test4,它将花费ID 8 =传递。
答案 0 :(得分:1)
SELECT * FROM Test_Results t
WHERE t.ID in (SELECT MAX(ID) FROM Test_Results GROUP BY Test_Name)
答案 1 :(得分:0)
我认为您应该获得最大ID而不是最大重试方式
select * from test_results
where (test_name, id) in select ( test_name, max(id) from test_results group by test_name) )
答案 2 :(得分:0)
获取测试名称的最大ID并获得其结果。
select * from test_results where id in
(select max(id) from test_results group by test_name);
+----+-----------+--------+-------+
| id | test_name | status | retry |
+----+-----------+--------+-------+
| 1 | test1 | passed | 0 |
| 3 | test2 | passed | 1 |
| 5 | test3 | failed | 1 |
| 8 | test4 | passed | 2 |
+----+-----------+--------+-------+