从所有Record-Sql获取第一行

时间:2011-02-28 14:21:56

标签: sql

我有一个包含

的表格
Reg_Num    Name       Cash        Date

1          John       500        2011-2-12
1          lisak      600        2011-2-12
1          Joe        300        2011-2-11
2          Josh       700        2011-2-10
2          Mark       200        2011-2-12

现在我希望输出为每个Reg_Num的第一条记录 输出:

1          John        500       2011-2-12
2          JOsh        700       2011-2-10

谢谢

1 个答案:

答案 0 :(得分:2)

假设SQL Server 2005 +:

;with cteMinDate as (
    select Reg_Num, Name, Cash, Date,
           row_number() over (partition by Reg_Num order by Date) as RowNum
        from YourTable
)
select Reg_Num, Name, Cash, Date
    from cteMinDate
    where RowNum = 1