【发布时间】:2020-03-14 23:18:33
【问题描述】:
我正在运行此代码来选择 sql 中每个数据库的排名。但是,我没有得到我想要的结果。
数据库: 代码:
SELECT user_id,country,city,institute,
RANK () OVER (PARTITION BY country ORDER BY up_vote+down_vote DESC) country_rank,
RANK() OVER (PARTITION BY city ORDER BY up_vote+down_vote DESC) city_rank,
RANK() OVER (PARTITION BY institute ORDER BY up_vote+down_vote DESC) institute_rank
FROM Users ;
FROM Users;
user_id | country | city | institute | country_rank | city_rank | institute_rank
---------+---------+---------+----------------------+--------------+-----------+----------------
17 | Canada | Toronto | University of Ottawa | 1 | 1 | 1
18 | Canada | Ottawa | University of Ottawa | 2 | 1 | 2
16 | test123 | test123 | test123 | 1 | 1 | 1
我只想获取 user_id=18 所以我添加了
WHERE user_id=18
但是,我得到了这个结果
user_id | country | city | institute | country_rank | city_rank | institute_rank
---------+---------+--------+----------------------+--------------+-----------+----------------
18 | Canada | Ottawa | University of Ottawa | 1 | 1 | 1
我想要什么作为我的结果
user_id | country | city | institute | country_rank | city_rank | institute_rank
---------+---------+--------+----------------------+--------------+-----------+----------------
18 | Canada | Ottawa | University of Ottawa | 2 | 1 | 2
【问题讨论】:
标签: sql postgresql window-functions rank