7

请教个查询排名 RANK 的 sql 写法

 2 years ago
source link: https://www.v2ex.com/t/799971
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  PostgreSQL

请教个查询排名 RANK 的 sql 写法

  zxCoder · 6 小时 18 分钟前 · 459 次点击
SELECT userId, MIN(time), RANK() OVER (ORDER BY MIN(time) ASC) AS
 "rank" FROM game  WHERE a = 1 AND b = 2 AND c = 3 GROUP BY userId

这个 sql 能查出按 user 分组之后每个 user 最小的 time 和 rank,但是如果我想只查询其中某个 user 的 rank,应该怎么写呢?还是全部读取再自己 for 一遍?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK