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, 
   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 に答える
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 に答える