gpt4 book ai didi

sql - 如何根据年份和月份进行排名?

转载 作者:行者123 更新时间:2023-12-04 20:33:19 24 4
gpt4 key购买 nike

我正在尝试根据年份和月份放置排名。我有类似的记录

产品

ProdID  Years  Months  Quantity
--------------------------------
1652 2018 10 2
1010 2018 9 2
2236 2018 10 2
2236 2018 10 2
1445 2019 5 2
1789 2019 12 2
1232 2018 12 2

我尝试了以下查询

SELECT
ProdID, Years, Months, Quantity,
DENSE_RANK()OVER (Partition By Years Order By Months) Ranks
FROM Products

显然,结果会是

ProdID  Years  Months  Quantity   Ranks
---------------------------------------
1010 2018 9 2 1
1652 2018 10 2 2
2236 2018 10 2 2
2236 2018 10 2 2
1232 2018 12 2 3
1445 2019 5 2 1
1789 2019 12 2 2

期待

ProdID  Years  Months  Quantity   Ranks
---------------------------------------
1010 2018 9 2 1
1652 2018 10 2 2
2236 2018 10 2 2
2236 2018 10 2 2
1232 2018 12 2 3
1232 2018 12 2 3
1445 2019 5 2 4
1789 2019 12 2 5

最佳答案

您需要从查询中删除 partition by 子句。只需使用 order by 子句。

SELECT ProdID, Years, Months, Quantity,
DENSE_RANK()OVER (Order By Years, Months) Ranks
FROM Products

关于sql - 如何根据年份和月份进行排名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57248063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com