Postgres - Select 天仅处于 FAILURE 状态

Postgres - Select days with FAILURE status only

我有 table 每天有多个特定 project_name 的结果(成功、失败、中止、不稳定)。我只想 select 只出现 FAILURE 状态的日子 - 没有别的。有些日子有多个状态(失败后可以解决并获得不同时间的状态并设置为成功)。 我怎样才能 select 只有失败状态的日子?

我的查询:

SELECT
    date_trunc('day', time) AS group_day,
    build_result,
    project_name
FROM project_status
WHERE project_name = 'project_x'
GROUP BY date_trunc('day', time), project_name, build_result

使用聚合和具有:

SELECT date_trunc('day', time) AS group_day, project_name
FROM project_status
WHERE project_name = 'project_x'
GROUP BY date_trunc('day', time), project_name
HAVING MIN(build_result) = MAX(build_result) AND
       MIN(build_result) = 'FAILURE'

可以按group_dayproject_name分组,在HAVING子句中使用string_agg()设置条件build_result的唯一值是'FAILURE':

SELECT date_trunc('day', time) AS group_day,
       project_name
FROM project_status
WHERE project_name = 'project_x'
GROUP BY group_day, project_name
HAVING string_agg(DISTINCT build_result, ',') = 'FAILURE'