通过最新日期获取最新结果,其中三列在sql中具有相同的值

问题描述 投票:0回答:1

我是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中的某些列将产生非聚合的列错误。

mysql sql database greatest-n-per-group
1个回答
0
投票

您可以过滤而不是汇总;

select t.*
from mytable
where t.testdate = (
    select max(t1.testdate) from mytable t1 where t1.service = t.service
)
© www.soinside.com 2019 - 2024. All rights reserved.