【问题标题】:calculating urban population percentage of total population for each district in PostgreSql在 PostgreSql 中计算每个地区的城市人口占总人口的百分比
【发布时间】:2018-03-12 17:30:14
【问题描述】:

我有一个表,即结算由 3 列和 5437 行组成。 第一列有 8 组(A、B、C、D、E、F、G、H),第二列有 '6' 组(City,Town,Village,Island,Suburb,Hamlet)强>

我的表如下...

ID     district_name              Cluster         Population    

1003        A                    village            1543          
1002        B                    town               63437   
1003        C                    village            1698    
1002        D                    town               11785   
1040        E                    hamlet             100 
2034        F                    hamlet             190 
4003        G                    city               207561  
4243        H                    Suburb             682 
674         D                    Island             718 
1012        A                    village            1566
1040        F                    hamlet             200
1020        D                    town               11881
4055        D                    city               208763  

我想根据以下条件计算每个地区的人口百分比;

total population in each district/sum(population)*100
where cluster='city' or cluster='town'

A practical example of my desire outpout can be as follows,

district_name  Population_total(City+Town+island) Urban_Percentage(Town_City)


     D                233147                      99.69(232429/233147 * 100)

【问题讨论】:

标签: sql postgresql


【解决方案1】:

SQL DEMO

WITH  district as (         
     SELECT district_name, 
            sum("Population") as district_total
     FROM residentioal 
     where "Cluster"='city' or "Cluster"='town'
     GROUP BY district_name
), whole_total as (
     SELECT district_name, sum("Population") as total
     FROM residentioal 
     GROUP BY district_name
 )
SELECT d.district_name, 
       total as whole_total,
       district_total as city_town_total,        
       district_total * 100.0 / total as PP
FROM district d
JOIN whole_total  w
  ON d.district_name = w.district_name

输出

| district_name | whole_total | city_town_total |                pp |
|---------------|-------------|-----------------|-------------------|
|             D |      233147 |          232429 | 99.69203978605772 |
|             B |       63437 |           63437 |               100 |
|             G |      207561 |          207561 |               100 |

编辑:

SQL DEMO

您实际上可以通过单个查询来完成:

SELECT   district_name,
         sum("Population") as total,
         sum(CASE WHEN "Cluster" IN ('city', 'town') 
                  THEN "Population"
                  ELSE 0
             END ) as district_total,
         sum(CASE WHEN "Cluster" IN ('city', 'town') 
                  THEN "Population"
                  ELSE 0
             END )  * 100.0 / sum("Population") as PP
FROM residentioal
GROUP BY district_name;

【讨论】:

  • 请再试一次
  • 检查新版本。
  • 感谢您的友好回答。实际上,我希望“city_tow_total”列成为所有集群(城市+城镇+小村庄+村庄+岛屿+每个地区的郊区)的总和,而不仅仅是(城市和城镇)。因此,公式为;城镇人口总和/所有集群中所有人口的总和 *100 注意:对于每个地区,我们有不同的总人口数。如果你能回答我,我将不胜感激。对不起,我的解释不够好
  • 我告诉过你我需要这些数字。否则是一个猜谜游戏,会浪费我的时间来猜测你需要什么:(
  • 亲爱的@Juan Carlos Oropeza 我添加了一个新的编辑作为表格格式的期望输出。这是唯一需要澄清的事情:)希望我解释得准确
猜你喜欢
  • 2016-02-18
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多