将CASE WHEN应用于MIN / MAX

时间:2017-07-27 15:23:32

标签: sql amazon-redshift

我正在尝试根据两个条件将MIN/MAX应用于我的SELECT子句。 (见下文)。当我尝试在另一个查询中引用我的列submitted_at时,它表示列引用不明确。

MIN(CASE WHEN (document != 'dphoto' AND document_type != 'dlicenses' AND 
status = 'upload') THEN occurred_at END) AS submitted_at,`

MAX(CASE WHEN (document_type = 'dphoto' AND document_type = 'dlicenses' AND
status = 'upload') THEN occurred_at END) AS submitted_at`  

我试图将这个短语合并为1个选择,但一直没有成功。感谢任何提示

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法将两者合并为一列:

CASE    WHEN    (document != 'dphoto' AND document_type != 'dlicenses' AND status = 'upload')
            THEN    Min(occurred_at)
        WHEN    (document_type = 'dphoto' AND document_type = 'dlicenses' AND status = 'upload') 
            THEN    Max(occurred_at)
END As Submitted_at