How can I pull only latest product made by Cash on every first payment done? 1 transaction have unique ID and can purchase few products in different time. I tried to pull as below but it still pull out Cash.
select id,Payment, max(PaymentDate), PaymentMethod from table name where PaymentMethod ='Cash' group by select id,Payment,PaymentMethod
Senthil Kumar C ------------------------------------------------------ MCITP - Database Administration SQL SERVER 2008 MCTS - Database Development SQL SERVER 2008
What I meant is I just want the output for every first transaction which use Cash. ID 123 first transaction is Debit Card according to the payment date. It should not appear in the output.
select *
from
(
select *, rn = row_number() over (partition by ID order by PaymentDate desc)
from yourtable
where PaymentMethod = 'Cash'
) d
where d.rn = 1
and PaymentMethod = 'Cash'