Multiplying two columns which have been calculated on a CASE statement

后端 未结 2 1653
礼貌的吻别
礼貌的吻别 2021-01-23 04:52

I am performing a SQL query in PostgreSQL using a CASE statement like this:

SELECT
    CASE column1
        WHEN something THEN 10
        ELSE 20
          


        
2条回答
  •  鱼传尺愫
    2021-01-23 05:30

    You can always use a CTE to abstract things away to a different level, if that helps - something along the lines of ...

    With CTE as
    (
     SELECT
      CASE column1
        WHEN something THEN 10
        ELSE 20
        END AS newcol1,
      CASE column12
        WHEN something THEN 30
        ELSE 40
        END AS newcol2,
      column3,
     FROM table
    )
    SELECT
      newcol1, newcol2,
      count(column3) as newcol3,
     (newcol2 * newcol3) AS newcol4
    FROM CTE 
    GROUP BY newcol1,newcol2,newcol3
    

提交回复
热议问题