You can use RANK()
, partitioning on date
and id
and ordering by value
descending:
(您可以使用RANK()
,对date
和id
分区,并按value
降序排序:)
SELECT *,
RANK() OVER (PARTITION BY date, id ORDER BY value DESC) AS ranking
FROM data
Output:
(输出:)
date id value ranking
01-01-2018 A 50 1
01-01-2018 A 20 2
01-01-2018 B 40 1
01-01-2018 C 40 1
02-01-2018 A 30 1
03-01-2018 C 40 1
03-01-2018 C 20 2
04-01-2018 B 40 1
04-01-2018 B 0 2
05-01-2018 B 70 1
Demo on SQLFiddle
(关于SQLFiddle的演示)
This query will run on all the DBMS you have tagged your question with.
(该查询将在您标记了问题的所有DBMS上运行。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…