问题描述
表格:警报
已创建,已确认,已忽略
如果AckNowledgedDate和IgnoredDate均为空,则警报被认为是活动的。如果AckNowledgedDate不为null但IgnoredDate为null,则认为已确认。如果IgnoredDate不为null,则认为已忽略。
对于我的查询,我需要按照活动->确认->忽略->创建的顺序来订购警报。因此,除了在比较两个处于活动状态或已确认或忽略状态的警报时使用的CreatedOn,我不在乎任何实际日期。
SELECT alrt FROM ALERT alrt ORDER BY ???
解决方法
如果我理解正确,则可以使用case
逻辑:
order by (case when AcknowledgedDate is null and IgnoredDate then 1
when IgnoredDate is null then 2
else 3
end)
,
根据您的条件,这样做:
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