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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Grab latest

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 Date
Visa $50 2012-05-05
Cash $100 2012-05-16

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-22 : 10:33:07
[code]
select top 1 PaymentMethod
from your_data
order by PaymentDate desc
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-05-22 : 10:34:38
I meant grab the latest date
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-22 : 10:42:32
[code]
select top 1 PaymentMethod, PaymentDate
from your_data
order by PaymentDate desc
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

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 Date
Heli Visa $50 2012-05-05
Heli Cash $100 2012-05-16
Heli Visa $50 2012-05-05
John Cash $100 2012-05-04
John Visa $50 2012-05-03
John Cash $100 2012-05-02

I only want to grab those with latest date.

Name PaymentMethod Payment Date
Heli Cash $100 2012-05-16
John Cash $100 2012-05-04
Go to Top of Page

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
) d
where rn = 1



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -