Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算Postgresql中每个年龄组的频率_Sql_Postgresql_Group By_Sql Order By - Fatal编程技术网

计算Postgresql中每个年龄组的频率

计算Postgresql中每个年龄组的频率,sql,postgresql,group-by,sql-order-by,Sql,Postgresql,Group By,Sql Order By,我有一个如下所示的输入数据 Person_id Age 21352471 59 22157363 51 22741394 75 22764902 27 22771872 62 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 我试图计算每

我有一个如下所示的输入数据

Person_id   Age
21352471    59
22157363    51
22741394    75
22764902    27
22771872    62
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 
我试图计算每个年龄组(如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;

你可以试试下面的方法-

select case when age<=10 then "0-10"
when age>10 and age<=20 then "11-20"
when age>20 and age<=30 then "21-30" end as age_group
count(person_id) as freq
from
  age_table
group by
case when age<=10 then "0-10"
when age>10 and age<=20 then "11-20"
when age>20 and age<=30 then "21-30" end  

你可以试试下面的方法-

select case when age<=10 then "0-10"
when age>10 and age<=20 then "11-20"
when age>20 and age<=30 then "21-30" end as age_group
count(person_id) as freq
from
  age_table
group by
case when age<=10 then "0-10"
when age>10 and age<=20 then "11-20"
when age>20 and age<=30 then "21-30" end  

下面是一种将年龄分成10个批次的方法,如下所示

select concat(
        (age/10)*10 
       ,'-'
       ,(age/10)*10+10
        )as age_bracket
      ,count(person_id) as frequency
 from t
group by concat(
        (age/10)*10 
       ,'-'
       ,(age/10)*10+10
        )
order by 1  


+-------------+-----------+
| age_bracket | frequency |
+-------------+-----------+
| 20-30       |         1 |
| 50-60       |         2 |
| 60-70       |         1 |
| 70-80       |         1 |
+-------------+-----------+
双联杆


下面是一种将年龄分成10个批次的方法,如下所示

select concat(
        (age/10)*10 
       ,'-'
       ,(age/10)*10+10
        )as age_bracket
      ,count(person_id) as frequency
 from t
group by concat(
        (age/10)*10 
       ,'-'
       ,(age/10)*10+10
        )
order by 1  


+-------------+-----------+
| age_bracket | frequency |
+-------------+-----------+
| 20-30       |         1 |
| 50-60       |         2 |
| 60-70       |         1 |
| 70-80       |         1 |
+-------------+-----------+
双联杆


如果你想展示0-100岁之间的所有年龄段,无论是否有参赛作品,这里还有另一个答案

with data
  as (select concat(
                   case when x=1 then 0 else (x-1)*10+1 end
                   ,'-'
                   ,x*10
                  ) as ranges
             ,case when x=1 then 0 else (x-1)*10+1 end lv
             ,x*10 as hv
        from generate_series(1,10) x
      ) 
    select d.ranges as age_bracket
          ,count(t.person_id) as frequency
     from data d
left join t 
       on t.age>=d.lv
      and t.age<=d.hv
group by d.ranges 
order by 1

-------------+-----------+
| age_bracket | frequency |
+-------------+-----------+
| 0-10        |         0 |
| 11-20       |         0 |
| 21-30       |         1 |
| 31-40       |         0 |
| 41-50       |         0 |
| 51-60       |         2 |
| 61-70       |         1 |
| 71-80       |         1 |
| 81-90       |         0 |
| 91-100      |         0 |
+-------------+-----------+
小提琴链接


如果你想展示0-100岁之间的所有年龄段,无论是否有参赛作品,这里还有另一个答案

with data
  as (select concat(
                   case when x=1 then 0 else (x-1)*10+1 end
                   ,'-'
                   ,x*10
                  ) as ranges
             ,case when x=1 then 0 else (x-1)*10+1 end lv
             ,x*10 as hv
        from generate_series(1,10) x
      ) 
    select d.ranges as age_bracket
          ,count(t.person_id) as frequency
     from data d
left join t 
       on t.age>=d.lv
      and t.age<=d.hv
group by d.ranges 
order by 1

-------------+-----------+
| age_bracket | frequency |
+-------------+-----------+
| 0-10        |         0 |
| 11-20       |         0 |
| 21-30       |         1 |
| 31-40       |         0 |
| 41-50       |         0 |
| 51-60       |         2 |
| 61-70       |         1 |
| 71-80       |         1 |
| 81-90       |         0 |
| 91-100      |         0 |
+-------------+-----------+
小提琴链接


PostgreSQL支持按1分组:PostgreSQL支持按1分组:@TheGreat。Postgres允许您在group by中重复列别名,这有点简化。@TheGreat。Postgres允许您在GroupBy中重复列别名,这有点简化。