获取总数:如何从本《守则》中按性别或身体状况按省获取男女总数?

时间:2018-07-12 10:39:17

标签: sql postgresql

如何从本《守则》中按性别或身体状况按省获取男女总数?

SELECT sv.province_name, sv.district_name,
          sum(sexual_female_less_10) as sexual_female_less_10,
          sum(sexual_female_10_14) as sexual_female_10_14,
          sum(sexual_female_15_19) as sexual_female_15_19,
          sum(sexual_female_20_24) as sexual_female_20_24,
          sum(sexual_female_25_plus) as sexual_female_25_plus,
          sum(sexual_male_less_10) as sexual_male_less_10,
          sum(sexual_male_10_14) as sexual_male_10_14,
          sum(sexual_male_15_19) as sexual_male_15_19,
          sum(sexual_male_20_24) as sexual_male_20_24,
          sum(sexual_male_25_plus) as sexual_male_25_plus,
          sum(physical_female_less_10) as physical_female_less_10,
          sum(physical_female_10_14) as physical_female_10_14,
          sum(physical_female_15_19) as physical_female_15_19,
          sum(physical_female_20_24) as physical_female_20_24,
          sum(physical_female_25_plus) as physical_female_25_plus,
          sum(physical_male_less_10) as physical_male_less_10,
          sum(physical_male_10_14) as physical_male_10_14,
          sum(physical_male_15_19) as physical_male_15_19,
          sum(physical_male_20_24) as physical_male_20_24,
          sum(physical_male_25_plus) as physical_male_25_plus

   FROM vw_experiencedphysicalviolence pv
   JOIN vw_experiencedsexualviolence sv ON pv.id = sv.id
   GROUP BY sv.district_name, sv.province_name

2 个答案:

答案 0 :(得分:1)

一些细节。

  1. GROUP BY子句中,您将按地区和省进行分组。在您的问题中,指定省。如果正确的话,您的分组依据应该像这样GROUP BY sv.province_name
  2. 发布SQL问题时,它有助于标记您正在使用的数据库引擎。在这种情况下,答案很可能包含在ANSI标准中。但是,更高级的查询可能会依赖于供应商特定的SQL实现。
  3. 如果要按省汇总男性和女性的总数,则可以对各列求和,然后对合计值求和。

请参见下面的示例代码:

SELECT 
sv.province_name, 
sv.district_name, -- Remove if you remove from group by clause
sum(sexual_female_less_10) +
sum(sexual_female_10_14) +,
sum(sexual_female_15_19) +
sum(sexual_female_20_24) +
sum(sexual_female_25_plus) AS total_f_sexual_violence,  
sum(physical_female_less_10) +
sum(physical_female_10_14) +
sum(physical_female_15_19) +
sum(physical_female_20_24) +
sum(physical_female_25_plus) AS total_f_physical_violence,  
sum(sexual_male_less_10) +
sum(sexual_male_10_14) +
sum(sexual_male_15_19) +
sum(sexual_male_20_24) +
sum(sexual_male_25_plus) AS total_m_sexual_violence,
sum(physical_male_less_10) +
sum(physical_male_10_14) +
sum(physical_male_15_19) +
sum(physical_male_20_24) +
sum(physical_male_25_plus) AS total_f_physical_violence
FROM vw_experiencedphysicalviolence pv
JOIN vw_experiencedsexualviolence sv ON pv.id = sv.id
GROUP BY 
sv.district_name, -- Per your question you may not want to group by district_name  
sv.province_name

答案 1 :(得分:0)

您可以尝试使用OVER子句。

即:

SELECT DISTINCT
    sv.province_name,
    sv.district_name,
    sum(sexual_female_less_10) OVER (partition by sv.province_name, sv.district_name) as District_sexual_female_less_10,
    sum(sexual_female_less_10) OVER (partition by sv.province_name) as Province_sexual_female_less_10,
...
FROM vw_experiencedphysicalviolence pv
JOIN vw_experiencedsexualviolence sv ON pv.id = sv.id

有关更多信息:https://docs.microsoft.com/en-us/sql/t-sql/functions/sum-transact-sql?view=sql-server-2017