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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Last Sold Query

Status
Not open for further replies.

UltraSmooth

Programmer
Oct 28, 2002
97
CA
I need to write a query selects all items from an inventory table 'MenuItem' that have sold since a user specified date. The sales table 'Custinv' has a record for every item sold by date and location (quite a large amount of records).

I'm not really sure how to proceed, should I be joining with the sales table? Won't this procedure have to do a full table scan to find the latest date the item was sold?

I was thinking maybe I had to query the sales table to bring back a list of items and the last date they were sold and then join to the inventory table to get the item detail information.

Needless to say, I'm a little confused. Any help would be appreciated.
 
Code:
SELECT a.* FROM MenuItem a

INNER JOIN

(SELECT DISTINCT ItemID
FROM CustInv
WHERE SoldDate >= @SomeDate
) b

ON a.ItemID = b.ItemID
 
Here's what I've come up with so far, and I just noticed it's very similar to Riverguy's response,

Code:
SELECT MenuItem.*
	, sales.*
FROM MenuItem
JOIN (
SELECT DISTINCT custitem.PLU
	, custitem.LocationNo
	, MAX(Custinv.InvoiceDate) AS LastSold
FROM Custinv JOIN custitem 
  ON Custinv.InvoiceNo=custitem.InvoiceNo
 AND Custinv.LocationNo=custitem.LocationNo
WHERE Custinv.InvoiceDate>='06-01-2009'
GROUP BY custitem.PLU
	, custitem.LocationNo
) AS sales
ON MenuItem.PLU=sales.PLU
AND MenuItem.LocationNo=sales.LocationNo

Does my query make sense?
 
One more point, UltraSmooth asked "Won't this procedure have to do a full table scan to find the latest date the item was sold?"

An index can help performance.


djj
The Lord is My Shepard (Psalm 23) - I need someone to lead me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top