SQL GROUP BY CASE statement with aggregate function

后端 未结 4 1869
时光说笑
时光说笑 2020-11-29 02:47

I have a column that looks something like this:

CASE
    WHEN col1 > col2 THEN SUM(col3*col4)
    ELSE 0
END AS some_product

And I would

4条回答
  •  忘掉有多难
    2020-11-29 03:11

    I think the answer is pretty simple (unless I'm missing something?)

    SELECT    
    CASE
        WHEN col1 > col2 THEN SUM(col3*col4)
        ELSE 0
    END AS some_product
    FROM some_table
    GROUP BY
    CASE
        WHEN col1 > col2 THEN SUM(col3*col4)
        ELSE 0
    END
    

    You can put the CASE STATEMENT in the GROUP BY verbatim (minus the alias column name)

提交回复
热议问题