Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How i can get the first item from a table

Status
Not open for further replies.

Edimator

IS-IT--Management
Mar 13, 2001
49
VE
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!
 
First you can create a table temp as query
Code:
SELECT CUSTOMERID, MIN(SALEDATE) as FIRST FROM SALES
and create an index over CustomerId on this table.
Of course you should have one on table sales, same field.
And don't use
Code:
LIKE
on your query, use
Code:
=
. This way it should be faster.

Oh, I haven't revised your query, I've supposed it's correct.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top