带有日期的Row_Count和带有名称的重置计数

时间:2012-01-14 07:26:42

标签: sql sql-server sql-server-2008 sql-server-2005

我有以下数据

11/10/2011  john Maths
12/10/2011  Max  Geometry
11/10/2011  John Geometry
12/10/2011  Max  Maths

所以我想要以下结果

1 11/10/2011  john Maths
2 12/10/2011  John Geometry
1 11/10/2011  Max  Geometry
2 12/10/2011  John Geometry

2 个答案:

答案 0 :(得分:1)

select
    row_number() over(partition by [name] order by [date] asc, [subject] asc) as row,
    [date],
    [name],
    [subject]
from table_name

详细了解row_number() on MSDN

答案 1 :(得分:0)

您应该使用的是row_number() over (partition by name, order by name)

将此子句添加到您对学生的简单查询中。