I have a fact table with items solds and i need identify the serial number of the first item buyed by a customer.
Sales:
-----------------------------------
CustomerID, SerialNumber, SaleDate
-----------------------------------
000000001 1234567890 10/02/2001
000000001 2314321432 10/05/2001
000000001 3425345534 10/14/2001
000000001 4564565436 10/25/2001
000000002 4756476547 10/01/2001
I need a query that return:
000000001 1234567890 10/02/2001
000000002 4756476547 10/01/2001
I try with:
SELECT SALES.CUSTOMERID, SALES.SERIALNUMBER,
SALES.SALEDATE FROM SALES,
(SELECT CUSTOMERID, MIN(SALEDATE) as FIRST FROM SALES) TEMP
WHERE SALES.CUSTOMERID = TEMP.CUSTOMERID
AND SALES.SALEDATE LIKE TEMP.FIRST
My sales table has more than 2M rows, so i need a more optimus query.
I really apreciate any kind of help!
Sales:
-----------------------------------
CustomerID, SerialNumber, SaleDate
-----------------------------------
000000001 1234567890 10/02/2001
000000001 2314321432 10/05/2001
000000001 3425345534 10/14/2001
000000001 4564565436 10/25/2001
000000002 4756476547 10/01/2001
I need a query that return:
000000001 1234567890 10/02/2001
000000002 4756476547 10/01/2001
I try with:
SELECT SALES.CUSTOMERID, SALES.SERIALNUMBER,
SALES.SALEDATE FROM SALES,
(SELECT CUSTOMERID, MIN(SALEDATE) as FIRST FROM SALES) TEMP
WHERE SALES.CUSTOMERID = TEMP.CUSTOMERID
AND SALES.SALEDATE LIKE TEMP.FIRST
My sales table has more than 2M rows, so i need a more optimus query.
I really apreciate any kind of help!