我是sql新手,需要您的帮助。我的数据库中有测试结果数据,我想获取最新结果。
----------------------------------------------------------------------------------
| ID | TestDate | App | Service | Environment | Critical | High | Medium | Low |
|----------------------------------------------------------------------------------|
| 1 | 2020-03-01 | app | service-a | sit | 1 | 2 | 3 | 5 |
| 2 | 2020-03-02 | app | service-b | sit | 0 | 3 | 2 | 1 |
| 3 | 2020-03-03 | app | service-a | sit | 0 | 1 | 5 | 3 |
我只想获取服务a的最新结果,然后获取服务b的结果。我尝试过
SELECT MAX(TestDate), App, Service, Environment, Critical, High, Medium, Low from table
GROUP BY TestDate, App, Service, Environment, Critical, High, Medium, Low;
但是它仍然返回两个service-a值。删除GROUP BY
中的某些列将产生非聚合的列错误。
您可以过滤而不是汇总;
select t.*
from mytable
where t.testdate = (
select max(t1.testdate) from mytable t1 where t1.service = t.service
)