我有以下postgresql选择
SELECT
col1,
col2,
col3,
CASE
WHEN var = 1
col4,
col5,
col6
WHEN var = 2
col7,
col8,
col9
WHEN var =3
col10,
col11,
col12
END
FROM A JOIN B .....
所以我只想在 CASE WHEN 条件下选择不同的列 但是由于case只返回一个值,这个语句会返回一个错误,有没有办法使这个工作?
答案 0 :(得分:1)
您需要多个CASE
表达式:
SELECT col1, col2, col3,
(CASE WHEN var = 1 THEN col4
WHEN var = 2 THEN col7
WHEN var = 3 THEN col10
END),
(CASE WHEN var = 1 THEN col5
WHEN var = 2 THEN col8
WHEN var = 3 THEN col11
END),
(CASE WHEN var = 1 THEN col6
WHEN var = 2 THEN col9
WHEN var = 3 THEN col12
END)
FROM A JOIN B .....