sql - 按同一个联接表过滤时计算所有联接表行
问题描述
所以我正在查询一个表区,它与位置表具有一对多的关系。我想选择具有特定名称职位的地区并同时计算所有地区职位。我可以这样做吗?
使用当前代码
SELECT districts.*, COUNT(DISTINCT(positions.id)) as positions_count FROM "districts"
LEFT JOIN positions ON positions.id = districts.position_id
WHERE ("positions"."name" IN ($1, $2)) GROUP BY districts.id ORDER BY positions_count desc, "districts"."name" ASC
如果我在某个地区有 20 个职位,但只有 2 个被过滤,positions_count eq 2 也是,我希望它是 20
我尝试使用别名在同一张桌子上加入两次,但这给了我相同的结果
SELECT districts.*, COUNT(DISTINCT(positions_to_count.id)) as positions_count FROM "districts"
LEFT JOIN positions ON positions.id = districts.position_id
LEFT JOIN positions AS positions_to_count ON positions_to_count.id = districts.position_id WHERE ("positions"."name" IN ($1, $2)) GROUP BY districts.id ORDER BY positions_count desc, "districts"."name" ASC
解决方案
我想你只想要条件聚合:
SELECT d.*, COUNT(DISTINCT p.id) as positions_count,
COUNT(DISTINCT CASE WHEN p.name IN ($1, $2) THEN p.id END) as positions_name
FROM "districts" d LEFT JOIN
positions p
ON p.id = d.position_id
GROUP BY d.id
ORDER BY positions_count desc, d."name" ASC;
注意:如果您没有给定区域的重复项,则无需使用COUNT(DISTINCT)
,就COUNT()
足够了。
推荐阅读
- reactjs - React Router,删除某些路线中的导航/侧边栏
- unit-testing - 当测试文件被定义为模块时,使用堆栈调用的 hspec 定义的测试会引发错误
- excel - 当单元格仍在调整大小范围内时,保留数据的文本更改
- javascript - 当子道具动态变化时,对象作为 React 子对象无效
- javascript - 如何在node.js中将json数组数据插入mysql?
- c - 在结构内的指针上使用 free() 可以为整个结构释放内存
- python - 如何打开 Firefox 并将新配置文件设置为默认配置?(硒和 Python)
- python-3.x - 在 ubuntu 18.04 上恢复 python3.6
- tensorflow - 如何将学习从 tensorflow 1.14 转移到 tf 2?
- string - 用户输入字符串的数值