通过检查几个空值来排序

vojdkbi0  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(340)

我有一个包含以下时间戳列的表:
表:警报
创建、确认、忽略
如果acknowledgeddate和ignoreddate都为空,则认为警报处于活动状态。如果AcknowledDate不为null,但ignoreddate为null,则视为已确认。如果ignoreddate不为null,则认为忽略了它。
对于我的查询,我需要按“活动”->“确认”->“忽略”->“创建”的顺序排列警报。因此,除了createdon之外,我不关心任何实际日期,createdon用于比较两个同时处于活动、已确认或忽略状态的警报。

SELECT alrt FROM ALERT alrt ORDER BY ???
bxjv4tth

bxjv4tth1#

如果我理解正确,你可以用 case 逻辑:

order by (case when AcknowledgedDate is null and IgnoredDate then 1
               when IgnoredDate is null then 2
               else 3
          end)
ni65a41a

ni65a41a2#

根据您的情况:

SELECT * FROM ALERT 
ORDER BY 
  AcknowledgedDate IS NULL AND IgnoredDate IS NULL DESC,
  AcknowledgedDate IS NOT NULL AND IgnoredDate IS NULL DESC,
  IgnoredDate IS NOT NULL DESC

相关问题