-1

Hi I have a table with the following fields:

  • Customerid
  • Customerpaydate
  • Amount

I need help in retrieving most recent transaction but no repetition in of customer ID.

I would appreciate your help

2 Answers2

1

To pull out the most recent transaction par customer, a correlated subquery should get the job done:

select t.*
from mytable t
where customerpaydate = (
    select max(Customerpaydate) from mytable t1 where t1.customerid = t.customerid      
)
GMB
  • 216,147
  • 25
  • 84
  • 135
0

This will give you the max paydate

 SELECT MAX(customerpaydate) as d 
 FROM table

This will give you the max paydate by customer

 SELECT customerid, max(customerpaydate) as d 
 FROM table 
 GROUP BY customerid

The will give you the most recent transaction

 SELECT *
 FROM TABLE
 where customerpaydate = (SELECT MAX(customerpaydate) as d FROM table)

This will give you the most recent transaction of each customer

 SELECT *
 FROM TABLE
 JOIN (
    SELECT customerid, max(customerpaydate) as d 
    FROM TABLE 
    GROUP BY customerid
 ) AS S ON S.customerid = TABLE.customerid and S.d = TABLE.customerpaydate
Hogan
  • 69,564
  • 10
  • 76
  • 117