【发布时间】:2020-01-22 12:08:33
【问题描述】:
我正在尝试选择表top_teams_team 中的所有列,并获取hash_value 列的值计数。此处的 sql 语句部分起作用,因为它返回两列,hash_value 和 total。我仍然希望它也给我表格的所有列。
select hash_value, count(hash_value) as total
from top_teams_team
group by hash_value
在下面的 sql 语句中,它给了我所有的列,但是显示了重复的 hash_value,这不是我想要的。我尝试输入 distinct 关键字,但它无法正常工作,或者我没有把它放在正确的位置。
select *
from top_teams_team
inner join (
select hash_value, count(hash_value) as total
from top_teams_team
group by hash_value
) q
on q.hash_value = top_teams_team.hash_value
【问题讨论】:
-
示例数据在这里会有所帮助。
-
如果您希望“所有列”伴随一个聚合字段,您必须定义要从哪些聚合行中选择其他列。为了获得最佳查询,您还必须披露 Postgres 版本、表定义、行数以及每个
hash_value预计有多少行?
标签: sql postgresql count greatest-n-per-group window-functions