如何使用SQL在另一列中按值聚合数据

时间:2019-07-05 06:57:40

标签: sql postgresql

我在PostgreSQL中有一个表,其中包含我所在国家/地区的人口统计数据。

df.loc[1]

如您所见,每个专业都重复使用Province_names。 那么,如何才能使省名不再重复,而只能在单独的栏中获得专业名称?

2 个答案:

答案 0 :(得分:1)

听起来好像您想旋转表(真的:最好在问题中显示数据和期望的输出!)


demo:db<>fiddle

这是PostgreSQL自9.4起使用FILTER子句的方式

SELECT
    province,
    SUM(people) FILTER (WHERE profession = 'teacher') AS teacher,
    SUM(people) FILTER (WHERE profession = 'banker') AS banker,
    SUM(people) FILTER (WHERE profession = 'supervillian') AS supervillian
FROM mytable
GROUP BY province

如果您想采用更通用的方法,则可以使用CASE子句

SELECT
    province,
    SUM(CASE WHEN profession = 'teacher' THEN people ELSE 0 END) AS teacher,
    SUM(CASE WHEN profession = 'banker' THEN people ELSE 0 END) AS banker,
    SUM(CASE WHEN profession = 'supervillian' THEN people ELSE 0 END) AS supervillian
FROM mytable
GROUP BY province

答案 1 :(得分:0)

您想要做的是一个枢轴,在Postgresql中要比在其他rdbms中复杂一些。您可以使用交叉表功能。在此处找到介绍:https://www.vertabelo.com/blog/technical-articles/creating-pivot-tables-in-postgresql-using-the-crosstab-function

对您来说,它看起来像这样:

SELECT * 
FROM crosstab( 'select Province_name, professions, Number_of_people from table1 order by 1,2') 
     AS final_result(Province_name TEXT, data_scientist NUMERIC,data_engineer NUMERIC,data_architect NUMERIC,student NUMERIC);