0

这是我目前的数据

FileNumber | Type | PerYear | PerMonth
L200200      Boat   2013      1
L303000      Boat2  2013      1
L400400      Yack   2013      2
L500500      Comp   2013      2
L600600      Item   2013      3
...
L909000      Chair  2014      1
X900220      Battle 2014      2

我想要一个基于 PerYear 和 PerMonth 的 Identity(counter) 列的查询这是我想要的结果

 KindOFIdentity FileNumber | Type | PerYear | PerMonth
 1              L200200      Boat   2013      1
 1              L303000      Boat2  2013      1
 2              L400400      Yack   2013      2
 2              L500500      Comp   2013      2
 3              L600600      Item   2013      3
 4 ...
 5 ...
 6 ...
 7 ...
 8 ...
 9 ...
 10 ...
 11 ...
 12 ... 
 13             L909000      Chair  2014      1
 14             X900220      Battle 2014      2

下面的查询没有给我正确的结果。

Select ROW_NUMBER() over (partition by PerYear,PerMonth order by PerYear, PerMonth) AS KindOFIdentity, Type, PerYear, PerMonth
from MyTable
4

2 回答 2

6

DENSE_RANK()不带PARTITION从句使用,

SELECT *other columns*, DENSE_RANK() OVER (ORDER BY PerYear, PerMonth) FROM dbo.MyTable;
于 2013-10-08T14:41:12.130 回答
-1

你不需要分区。只需使用 ROW_NUMBER OVER(ORDER BY PerYear, PerMonth) 它将返回 ROW_NUMBER 从 1 到 N 的结果集,其中 N 是行数。

于 2013-10-08T14:37:24.833 回答