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
精彩评论