我希望如果用户年龄>> 20,Columns1和columns2 sum收集并显示
我写了以下代码,但错误:
SELECT SUM(CASE WHEN age>20 THEN col1+col2+col3 ELSE 0 END) AS 'result1',
SUM(CASE WHEN age=20 THEN col1+col2+col3 ELSE 0 END) AS 'result2',
SUM(CASE WHEN age<20 THEN col1+col2+col3 ELSE 0 END) AS 'result3'
FROM users
怎么解决这个?
答案 0 :(得分:0)
我会做以下事情,我认为你首先需要更多的括号,我不知道你为什么要引用列名,所以我放弃了。
SELECT
SUM((CASE WHEN age > 20 THEN col1 + col2 + col3 ELSE 0 END)) as result1,
SUM((CASE WHEN age = 20 THEN col1 + col2 + col3 ELSE 0 END)) as result2,
SUM((CASE WHEN age > 20 THEN col1 + col2 + col3 ELSE 0 END)) as result3
FROM
"users"