Reputation: 474
I have a table where i store the Customer id and the date they logged in.
Cust_ID REC_DATE
773209 11/5/2013 4:30:52 PM
817265 11/5/2013 4:31:19 PM
And so on
How can i see only the latest two records by date for each customer?
Upvotes: 1
Views: 1122
Reputation: 1269873
You can use the analytic function row_number()
:
select t.*
from (select t.*,
row_number() over (partition by cust_id order by rec_date desc) as seqnum
from yourtable t
) t
where seqnum <= 2;
Upvotes: 3