列函数“MAX”的操作数包括列函数,标量全查询或子查询。

时间:2017-11-27 18:24:19

标签: sql db2 subquery pivot

我想按年龄和县来获得一些人。下面是我想要实现的图片。就像数据透视表enter image description here

一样
SELECT 
country,
MAX (CASE WHEN Age = '0' THEN  count(age) END) AS total0,
MAX (CASE WHEN Age = '1' THEN  count(age) END) AS total1,
MAX (CASE WHEN Age = '2' THEN  count(age) END) AS total2,
MAX (CASE WHEN Age = '3' THEN  count(age) END) AS total3,
MAX (CASE WHEN Age = '4' THEN  count(age) END) AS total4,
MAX (CASE WHEN Age = '5' THEN  count(age) END) AS total5
FROM (

SELECT person_id,
TIMESTAMPDIFF(256,CHAR(TIMESTAMP(a.admission_date) - TIMESTAMP(p.dateofbirth))) AS Age, 
dateofbirth,
a.admission_date

FROM person p
LEFT JOIN admission a ON p.person_id = a.person_id
WHERE category = '1' AND startdate BETWEEN '8/1/2016' AND '10/31/2017') AS a 

WHERE age <= '5'

GROUP BY country

此代码给出了这个错误:列函数“MAX”的操作数包括列函数,标量全查询或子查询。请帮忙谢谢

0 个答案:

没有答案