我有一个如下所示的输入数据
Person_id Age
21352471 59
22157363 51
22741394 75
22764902 27
22771872 62
我试图计算每个年龄组的发病率(患者数量),如 0-10
, 11-20
, 21-30
等
你能帮我怎么做吗?
我在网上做了类似的尝试,但这没有帮助
select
person_id,
count(*) filter (where age<=10) as "0-10",
count(*) filter (where age>10 and age<=20) as "11-20",
count(*) filter (where age>20) as "21-30"
from
age_table
group by
person_id;
我希望我的输出如下所示
Age_group freq
0-10 0
11-20 0
21-30 1
31-40 0
41-50 0
51-60 2
61-70 1
71-80 1
3条答案
按热度按时间oymdgrw71#
你可以试试下面的-
baubqpgj2#
下面是一种方法,它将年龄分成10个批次,如下所示
D小提琴连杆
https://dbfiddle.uk/?rdbms=postgres_12&fiddle=720d56ae4428a3ddd25ecb5bdac3b7fa
cfh9epnr3#
如果你很想显示0-100岁之间的所有年龄段,不管是否有参赛者,这里有另一个答案
db小提琴链接
https://dbfiddle.uk/?rdbms=postgres_12&fiddle=d21a1ef85d017a3d891ec6f85269a381