开发者

Getting First Row from all Record - Sql

开发者 https://www.devze.com 2023-02-14 09:18 出处:网络
I have a table which contains Reg_NumNameCashDate 1John5002011-2-12 1lisak6002011-2-12 1开发者_StackOverflowJoe3002011-2-11

I have a table which contains

Reg_Num    Name       Cash        Date

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

now I want the output as First record from every Reg_Num output:

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

Thank You


Assuming 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
0

精彩评论

暂无评论...
验证码 换一张
取 消