【发布时间】:2020-05-12 02:10:41
【问题描述】:
有两个表:
表education_data(按衡量指标按年份列出的国家/地区列表)。
create table education_data
(country_id int,
indicator_id int,
year date,
value float
);
表格indicators(所有指标列表):
create table indicators
(id int PRIMARY KEY,
name varchar(200),
code varchar(25)
);
我想找出完全缺乏信息的国家数量最多的指标 即最大值(按国家/地区列出的缺失指标计数)
我已经在 excel 中解决了这个问题(通过按国家/地区计算数据透视表中的空白)
pivot table with count for missing indicators by country
我还没有想到我们的 SQL 查询会返回相同的结果。
我能够返回某个国家/地区缺失指标的数量,请阅读下面的查询,但不是针对所有国家/地区。
SELECT COUNT(*)
FROM education_data AS edu
RIGHT JOIN indicators AS ind ON
edu.indicator_id = ind.id and country_id = 10
WHERE value IS NULL
GROUP BY country_id
到目前为止,我已经尝试过交叉连接但没有成功。
【问题讨论】:
-
你有国家表吗?
-
@clamp 是的,我愿意。
标签: sql postgresql join count left-join