SQL Group患者的年龄范围和性别

时间:2013-01-25 15:42:04

标签: sql count group-by case

表是耐心的.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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;

答案 2 :(得分: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;

答案 3 :(得分: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;