2017-11-27 18 views
0

年齢と郡別の人数を取得しようとしています。以下は私が達成しようとしているものです。ただ、ピボットテーブルのようなenter image description here列関数 "MAX"のオペランドには、列関数、スカラ全選択、または副問合せが含まれます。

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」は列関数、スカラー全選択、または副問合せを含んでいます。

MAX(COUNT(*)) 

.....問題は、あなたの集計を集約しようとしているであるあなたに

答えて

1

に感謝役立つしかし、現実には、あなたが本当に唯一COUNT()にしたいとは全くMAX()を必要としないでください。

SELECT 
country, 
COUNT(CASE WHEN Age = '0' THEN person_id END) AS total0, 
COUNT(CASE WHEN Age = '1' THEN person_id END) AS total1, 
COUNT(CASE WHEN Age = '2' THEN person_id END) AS total2, 
COUNT(CASE WHEN Age = '3' THEN person_id END) AS total3, 
COUNT(CASE WHEN Age = '4' THEN person_id END) AS total4, 
COUNT(CASE WHEN Age = '5' THEN person_id 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 
関連する問題