【发布时间】:2021-07-17 06:50:10
【问题描述】:
我有一张如下表
| pid | patient_name | age | sex | dm | htn | ckd |
|---|---|---|---|---|---|---|
| 1 | one | 10 | m | 1 | 0 | 1 |
| 2 | two | 15 | m | 1 | 1 | 1 |
| 3 | three | 30 | m | 1 | 0 | 1 |
| 4 | four | 40 | f | 1 | 0 | 1 |
| 5 | five | 50 | f | 1 | 0 | 1 |
| 6 | six | 60 | f | 1 | 0 | 1 |
| 7 | seven | 70 | m | 1 | 0 | 1 |
我需要男性患者的输出计数如下
| ranges | dm_count | htn_count | ckd_count |
|---|---|---|---|
| 80+ | 0 | 0 | 0 |
| 20-29 | 0 | 0 | 0 |
| 50-59 | 0 | 0 | 0 |
| under 20 | 2 | 1 | 2 |
| 30-39 | 1 | 0 | 1 |
| 40-49 | 0 | 0 | 0 |
| 70-79 | 1 | 0 | 1 |
| 60-69 | 0 | 0 | 0 |
下面我提到了我使用的查询
select t.ranges, count(*) as occurences from (
select age,
case when age >= '0' and age < '20' then "under 20"
when age >= '20' and age < '30' then "20-29"
when age >= '30' and age < '40' then "30-39"
when age >= '40' and age < '50' then "40-49"
when age >= '50' and age < '60' then "50-59"
when age >= '60' and age < '70' then "60-69"
when age >= '70' and age < '80' then "70-79"
else "80+" end as ranges,sex
from patient_sample where sex='M') as t
group by t.ranges
提前致谢。
【问题讨论】:
-
你忘了问问题。在内部查询中选择未使用的年龄和性别没有意义;外部查询不使用它们
-
我需要男性患者的输出计数如下@CaiusJard
-
这不是问题
-
我需要上表中的 dm、htn 和 ckd 的年龄明智计数(总和)你能帮忙吗?
标签: mysql group-by mysql-workbench