SQL Server - 属于关键字段的行数

时间:2017-09-12 17:36:44

标签: sql-server

我如何从这张表中获得:

item_no
-------
A
A
A
A
B
C
C
C

到一个看起来像这样的视图:

item_no   occurrence
-------   ----------
A         1
A         2
A         3
A         4
B         1
C         1
C         2 
C         3

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以在分区上使用row_number,如下所示:

Select Item_No, Row_Number() over(partition by Item_no order by Item_No) as Occurrence from yourtable

答案 1 :(得分:-1)

SELECT item_no, Count(item_no) AS occurrence FROM TableName GROUP BY item_no;