sql group by with case语句

t1qtbnec  于 2021-07-24  发布在  Java
关注(0)|答案(5)|浏览(480)

我有一张像这样的table

  1. +-----------+-----------+---------------------+
  2. | Report_id | Status | Date |
  3. +-----------+-----------+---------------------+
  4. | 1 | Completed | 2020-06-07 12:20:00 |
  5. | 1 | Completed | 2020-06-07 12:22:00 |
  6. | 2 | Running | 2020-06-07 13:02:00 |
  7. | 2 | Completed | 2020-06-07 13:10:00 |
  8. | 3 | Completed | 2020-06-07 14:10:00 |
  9. | 3 | Failed | 2020-06-07 14:04:00 |
  10. +-----------+-----------+---------------------+

我需要按报表id将此数据分组。因此,如果组中的所有状态值都等于completed,则status is completed和date是组中的最大值。但如果组中有一个running或failed值,则status需要分别等于running和failed,date应该与此值匹配。
输出将如下所示。

  1. +-----------+-----------+---------------------+
  2. | Report_id | Status | Date |
  3. +-----------+-----------+---------------------+
  4. | 1 | Completed | 2020-06-07 12:22:00 |
  5. | 2 | Running | 2020-06-07 13:02:00 |
  6. | 3 | Failed | 2020-06-07 14:04:00 |
  7. +-----------+-----------+---------------------+

我怀疑我需要在某个地方使用case语句来获得这个输出,但我不知道怎么做。请帮忙。

rryofs0p

rryofs0p1#

您可以使用string\u agg将状态列的文本组合成一个字符串,并检查该字符串是否包含“running”或“failed”

  1. select report_id,
  2. case when STRING_AGG(status, '') like '%Running%' then 'Running'
  3. when STRING_AGG(status, '') like '%Failed%' then 'Failed'
  4. else 'Completed' end Status,
  5. max(date) Date
  6. from abc
  7. group by report_id
z0qdvdin

z0qdvdin2#

查看您的样本,您似乎需要每个报告的初始状态(基于日期)

  1. select m.*
  2. from my_table m
  3. inner join (
  4. select Report_id, min(date) min_date
  5. from my_table
  6. group by Report_id
  7. ) t on t.Report_id = m. and t.min_date = m.date
ykejflvf

ykejflvf3#

你可以试着用 row_number() ```
select * from
(
select report_id,status,date, row_number() over(partition by report_id order by
case when status in ('Running','Failed') then 1 else 2 end asc,Date desc) as rn
from tablename
)A where rn=1

jei2mxaa

jei2mxaa4#

不确定这是否是你要找的,也不知道这是否是完全运行。

  1. select report_id,
  2. case when q.failed_sum > 0 then 'Failed'
  3. when q.running_sum > 0 then 'Running'
  4. else 'Completed'
  5. end,
  6. max(date)
  7. from table inner join
  8. (
  9. select report_id,
  10. sum(case when status = 'Failed' then 1 end) as failed_sum,
  11. sum(case when status = 'Running' then 1 end) as running_sum,
  12. sum(case when status = 'Completed' then 1 end)as completed_sum
  13. from table
  14. group by report_id
  15. )q on report_id = q.report_id
  16. where status = (case when failed_sum > 0 then 'Failed'
  17. else when running_sum > 0 then 'Running'
  18. else then 'Completed'
  19. end)
  20. group by report_id
展开查看全部
mlnl4t2r

mlnl4t2r5#

你可以用 distinct on :

  1. select distinct on (record_id) t.*
  2. from t
  3. order by (case when status <> 'Completed' then 1 else 2 end),
  4. date desc;

相关问题