SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get Latest Transaction
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

peace
Constraint Violating Yak Guru

389 Posts

Posted - 09/25/2012 :  03:56:47  Show Profile  Reply with Quote
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.

ID Payment PaymentDate PaymentMethod
123 $20 2012-01-10 Debit Card
123 $50 2012-01-20 Cash
234 $10 2012-01-30 Cash
234 $80 2012-02-20 Visa

I would like output as below:
ID Payment PaymentDate PaymentMethod
234 $10 2012-01-30 Cash

senthil_nagore
Flowing Fount of Yak Knowledge

India
1007 Posts

Posted - 09/25/2012 :  04:07:39  Show Profile  Visit senthil_nagore's Homepage  Send senthil_nagore a Yahoo! Message  Reply with Quote
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
Go to Top of Page

peace
Constraint Violating Yak Guru

389 Posts

Posted - 09/25/2012 :  04:30:56  Show Profile  Reply with Quote
The output still there for ID 123

ID Payment PaymentDate PaymentMethod
123 $50 2012-01-20 Cash
234 $10 2012-01-30 Cash
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17584 Posts

Posted - 09/25/2012 :  04:36:38  Show Profile  Reply with Quote

select *
from
(
    select *, rn = row_number() over (order by PaymentDate desc)
    from   yourtable
    where  PaymentMethod = 'Cash'
) d
where d.rn = 1



KH
Time is always against us

Go to Top of Page

peace
Constraint Violating Yak Guru

389 Posts

Posted - 09/25/2012 :  04:59:09  Show Profile  Reply with Quote
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.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17584 Posts

Posted - 09/25/2012 :  05:55:49  Show Profile  Reply with Quote
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'



KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000