【发布时间】:2020-08-31 22:42:32
【问题描述】:
我有两个表 A 和 B,一对多的关系(B 有 A 的外键)。现在我需要按 B 中的列分组,但要计算 A 中的行数(不同)。是否可以不使用 COUNT (distinct A.Id)
SELECT b.column1,
Count(*) totalGroupCount,
Count(distinct a.id) CountOfA -- I dont want to this for perf reason
FROM A a JOIN B b on a.id = b.a_id
GROUP BY b.column1
【问题讨论】:
标签: sql postgresql join count query-optimization