Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-05-22 : 10:25:12
|
| How to grab the latest payment method?I have this data as below:PaymentMethod Payment DateVisa $50 2012-05-05Cash $100 2012-05-16 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-05-22 : 10:33:07
|
[code]select top 1 PaymentMethodfrom your_dataorder by PaymentDate desc[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-05-22 : 10:34:38
|
| I meant grab the latest date |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-05-22 : 10:42:32
|
[code]select top 1 PaymentMethod, PaymentDatefrom your_dataorder by PaymentDate desc[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-05-23 : 00:30:39
|
| But i have a list of different records.If i select top 1, it only return 1 records.Name PaymentMethod Payment DateHeli Visa $50 2012-05-05Heli Cash $100 2012-05-16Heli Visa $50 2012-05-05John Cash $100 2012-05-04John Visa $50 2012-05-03John Cash $100 2012-05-02I only want to grab those with latest date.Name PaymentMethod Payment DateHeli Cash $100 2012-05-16John Cash $100 2012-05-04 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-05-23 : 00:48:49
|
maybe you should have mention it earlier ?select *from( select *, rn = row+number() over (partition by Name order by PaymentDate desc) from your_data) dwhere rn = 1 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|