我有如下疑问:
SELECT
IsTestBatch,
BatchID,
CampaignID,
IF(ActualTriggerDateTime IS NOT NULL,
MIN(ActualTriggerDateTime),
MIN(TriggerDateTime)) AS NextQueuedOn
FROM
campaignbatches
WHERE
Status IN (2,3)
GROUP BY
CampaignID
ORDER BY
IF(ActualTriggerDateTime IS NOT NULL, ActualTriggerDateTime, TriggerDateTime) ASC
if
条件工作不正常,最小日期也不符合我的预期。我期望上面的记录,但它返回上一个图像的表中的第一条记录:
最佳答案
由于聚合函数MIN()
忽略NULL
值,您可以尝试从列actualTriggerDateTime中选择最小日期,如果此列上的所有值都为空,则从列triggerDateTime中选择最小值。
SELECT
IsTestBatch,
BatchID,
CampaignID,
IFNULL(MIN(ActualTriggerDateTime), MIN(TriggerDateTime)) AS NextQueuedOn
FROM
campaignbatches
WHERE
Status IN (2,3)
GROUP BY
IsTestBatch, BatchID, CampaignID
ORDER BY
NextQueuedOn ASC