我刚开始使用 PostgreSQL,我敢打赌我的 qs 有一个简单的答案。
我有这样的看法:
county - candidate - votes
Abbeville - John Kasich - 157
Abbeville - Ben Carson - 305
Abbeville - Ted Cruz - 876
Abbeville - Jeb Bush - 236
Abbeville - Hillary Clinton - 1501
Abbeville - Marco Rubio - 740
Abbeville - Bernie Sanders - 312
Abbeville - Donald Trump - 1353
Abbot - Hillary Clinton - 0
Abbot - Bernie Sanders - 1
Abington - Ben Carson - 53
Abington - John Kasich - 299
Abington - Bernie Sanders - 1352
我想写一个查询,返回每个县的选票最多的人,例如:
county - candidate - votes
Abbeville - Hillary Clinton - 1501
Abbot - Bernie Sanders - 1
Abington - Bernie Sanders - 1352
我试过这个查询,但不能返回正确的候选人:
select
distinct on (county) county , candidate ,max(votes) over(partition by county)
from primary_results_csv prc
group by county, candidate, votes
order by county
提前感谢您的帮助。