Hi Agnes,
>Hi All,
>
>I have a table that holds a time tabes status per customer.
>
>I would like to create an SELCT SQL that gives me the 5 most recent records per customer
>
>like
>
>customer, date,value
>A,2007-01-01,1
>A,2007-01-02,2
>A,2007-01-03,3
>A,2007-01-04,4
>A,2007-01-05,5
>A,2007-01-06,6
>A,2007-01-07,7
>A,2007-01-08,8
>B,2007-01-02,2
>B,2007-01-03,3
>B,2007-01-04,4
>B,2007-01-05,5
>B,2007-01-16,6
>B,2007-01-17,7
>B,2007-01-18,8
>C,2007-01-17,7
>C,2007-01-18,8
>
>Result set:
>A,2007-01-04,4
>A,2007-01-05,5
>A,2007-01-06,6
>A,2007-01-07,7
>A,2007-01-08,8
>B,2007-01-03,3
>B,2007-01-04,4
>B,2007-01-05,5
>B,2007-01-16,6
>B,2007-01-17,7
>C,2007-01-17,7
>C,2007-01-18,8
>
>
>Any idea how to achive this?
>
>Agnes
Wouldn't be value's customer B from 4 until 8? (you putted 3 until 7)
If your answer is yes. Try:
SELECT mA.*;
FROM myTable mA;
, (SELECT customer, MAX(value) maxValue;
FROM myTable;
GROUP BY customer;
) mB;
WHERE mA.customer = mB.customer;
AND mA.value > mB.maxValue - 5;
ORDER BY mA.customer, mA.value
Good luck!
Erick
Força Sempre!
Strength Always!