2

表是 patient.birthdate 和 patient.sex 我无法创建新表。

大家好,我的查询试图按性别和年龄范围列出患者。我目前有这个。

SELECT CASE
     WHEN patient.birthdate  > '01-jan-1988'  THEN '1-25' 
     WHEN patient.birthdate > '01-jan-1963' THEN '25-50'
     WHEN patient.birthdate > '01-jan-1938' THEN '50-75' 
     ELSE '75+' 
   END AS Age_Range, 
   COUNT(patient.sex) AS "M",
   (SELECT COUNT(patient.sex) FROM patient WHERE patient.sex = 'F' ) AS "F" 
FROM patient
WHERE patient.sex = 'M'
GROUP BY CASE 
       WHEN patient.birthdate > '01-jan-1988'  THEN '1-25' 
       WHEN patient.birthdate > '01-jan-1963' THEN '25-50'
       WHEN patient.birthdate > '01-jan-1938' THEN '50-75'
       ELSE '75+' 
     END
     ORDER BY Age_Range;

这是我的输出正确地对男性进行分组和计数,但对于女性,我在每个年龄范围(3538)中得到相同的数字,即女性患者的总数。

4

4 回答 4

3
SELECT CASE
  WHEN patient.birthdate  > '01-jan-1988'  THEN '1-25' 
  WHEN patient.birthdate > '01-jan-1963' THEN '25-50'
  WHEN patient.birthdate > '01-jan-1938' THEN '50-75' 
  ELSE '75+' 
END AS Age_Range, 
SUM(CASE WHEN patient.Sex = 'M' THEN 1 ELSE 0 END) AS Males,
SUM(CASE WHEN patient.Sex = 'F' THEN 1 ELSE 0 END) AS Females
FROM patient
GROUP BY CASE
  WHEN patient.birthdate  > '01-jan-1988'  THEN '1-25' 
  WHEN patient.birthdate > '01-jan-1963' THEN '25-50'
  WHEN patient.birthdate > '01-jan-1938' THEN '50-75' 
  ELSE '75+' 
END
于 2013-01-25T15:45:15.187 回答
0
SELECT CASE
     WHEN patient.birthdate  > '01-jan-1988'  THEN '1-25' 
     WHEN patient.birthdate > '01-jan-1963' THEN '25-50'
     WHEN patient.birthdate > '01-jan-1938' THEN '50-75' 
     ELSE '75+' 
   END AS Age_Range, 
   SUM(CASE WHEN patient.sex = 'M' THEN 1 ELSE 0 END) AS "M Count",  
   SUM(CASE WHEN patient.sex = 'F' THEN 1 ELSE 0 END) AS "F Count"
FROM patient
WHERE patient.sex = 'M'
GROUP BY CASE 
       WHEN patient.birthdate > '01-jan-1988' THEN '1-25' 
       WHEN patient.birthdate > '01-jan-1963' THEN '25-50'
       WHEN patient.birthdate > '01-jan-1938' THEN '50-75'
       ELSE '75+' 
     END
     ORDER BY Age_Range;

我也建议这样做(如果您的系统支持 CTE),我认为它更清晰(并且可能更快)

WITH NewAge AS
(
  SELECT CASE
       WHEN patient.birthdate > '01-jan-1988' THEN '1-25' 
       WHEN patient.birthdate > '01-jan-1963' THEN '25-50'
       WHEN patient.birthdate > '01-jan-1938' THEN '50-75' 
       ELSE '75+' 
     END AS Age_Range, 
     CASE WHEN patient.sex = 'M' THEN 1 ELSE NULL END AS "M",  
     CASE WHEN patient.sex = 'F' THEN 1 ELSE NULL END AS "F"  
  FROM patient
)
SELECT Age_Range, COUNT(M) AS "M Count", COUNT(F) AS "F Count"
FROM NewAge
GROUP Age_Range
ORDER BY Age_Range;
于 2013-01-25T15:45:29.533 回答
0

利用CASE

SELECT CASE
     WHEN patient.birthdate  > '01-jan-1988'  THEN '1-25' 
     WHEN patient.birthdate > '01-jan-1963' THEN '25-50'
     WHEN patient.birthdate > '01-jan-1938' THEN '50-75' 
     ELSE '75+' 
     SUM(CASE WHEN patient.Sex = 'M' THEN 1 ELSE 0) AS M,
     SUM(CASE WHEN patient.Sex = 'F' THEN 1 ELSE 0) AS F
   END AS Age_Range, 
FROM patient
WHERE patient.sex = 'M'
GROUP BY CASE 
       WHEN patient.birthdate > '01-jan-1988'  THEN '1-25' 
       WHEN patient.birthdate > '01-jan-1963' THEN '25-50'
       WHEN patient.birthdate > '01-jan-1938' THEN '50-75'
       ELSE '75+' 
     END
     ORDER BY Age_Range;
于 2013-01-25T15:46:59.133 回答
0
SELECT CASE
     WHEN patient.birthdate  > '01-jan-1988'  THEN '1-25' 
     WHEN patient.birthdate > '01-jan-1963' THEN '25-50'
     WHEN patient.birthdate > '01-jan-1938' THEN '50-75' 
     ELSE '75+' 
   END AS Age_Range, 
   COUNT(CASE WHEN patient.sex = 'M' THEN patient.sex END) AS "M Count",  
   COUNT(CASE WHEN patient.sex = 'F' THEN patient.sex END) AS "F Count"
FROM patient
GROUP BY CASE 
       WHEN patient.birthdate > '01-jan-1988'  THEN '1-25' 
       WHEN patient.birthdate > '01-jan-1963' THEN '25-50'
       WHEN patient.birthdate > '01-jan-1938' THEN '50-75'
       ELSE '75+' 
     END
     ORDER BY Age_Range;
于 2013-01-25T15:47:12.603 回答