我有一个表格,其中包含以下格式的数据
region gender age_group population
Dallas M 1 200
Dallas F 1 500
NY M 1 320
NY F 1 310
Dallas M 2 300
Dallas F 2 600
NY M 2 400
NY F 2 800
我正在寻找以以下格式输出数据的最佳查询
region/age_group 1 2
Gender M F M F
Dallas 200 500 300 600
NY 320 310 400 800
我的查询仅返回顶部列和区域。不知道如何将顶部列细分为如上所示的性别
SELECT distinct (region), a.group_one, b.group_two
FROM city_populations c
LEFT JOIN city_populations AS a ON a.age_group=1 AND c.age_group=a.age_group
LEFT JOIN city_populations AS b ON b.age_group=2 AND c.age_group=a.age_group