【问题标题】:How to classify a set of rows in SQL?如何在 SQL 中对一组行进行分类?
【发布时间】:2022-11-15 06:23:57
【问题描述】:

我正在尝试解决此 SQL 练习。但我不知道该怎么做。有人有好主意吗?

试试看:https://dbfiddle.uk/fQmZkOaw

【问题讨论】:

  • 你在哪里找到这个问题的?
  • 总是有2个步骤

标签: sql sql-server group-by


【解决方案1】:

就像是:

SELECT workflow
, case when min(status) = max(status) THEN min(status) -- if all status is same, set to status
     when min(case when status = 'Error' then 0 end) = 0 then 'Intederminate' -- if any status is error then intedermined
  else 'Running' end
FROM #ProcessLog
group by workflow

也许?

【讨论】:

  • 代码看起来很棒。我不知道你可以在像 STATUS 这样的字符串列上使用 max。我以为你只能在数字上使用 min max 。
  • 是的,它对所有类型都非常方便。只需要注意 NULL,因为它们会被聚合忽略
猜你喜欢
  • 2019-11-08
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 2019-03-04
相关资源
最近更新 更多