如何根据年份和月份进行排名?
How to make Ranking based on Year and Month?
我正在尝试根据年份和月份设置排名。我有像
这样的记录
产品
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
您需要从查询中删除分区依据子句。只需使用 order by 子句即可。
SELECT ProdID, Years, Months, Quantity,
DENSE_RANK()OVER (Order By Years, Months) Ranks
FROM Products
以下查询应该可以满足您的要求:
SELECT
ProdID, Years, Months, Quantity,
DENSE_RANK()OVER (ORDER BY CAST(CAST(MONTHS AS VARCHAR)+'-01-'+CAST(YEARS AS VARCHAR) AS DATE)) Ranks
FROM Products
我正在尝试根据年份和月份设置排名。我有像
这样的记录产品
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
您需要从查询中删除分区依据子句。只需使用 order by 子句即可。
SELECT ProdID, Years, Months, Quantity,
DENSE_RANK()OVER (Order By Years, Months) Ranks
FROM Products
以下查询应该可以满足您的要求:
SELECT
ProdID, Years, Months, Quantity,
DENSE_RANK()OVER (ORDER BY CAST(CAST(MONTHS AS VARCHAR)+'-01-'+CAST(YEARS AS VARCHAR) AS DATE)) Ranks
FROM Products