|
-
Jul 22nd, 2007, 11:27 AM
#1
Thread Starter
Frenzied Member
Help With SQL Query
I have a inventory Table with a list of item numbers.
I also have a sales table showing all sales. The following field are in the sales table
Quantity
DateSold
ItemNum
CustomerName
I need a to show all the items of the inventory table together with the last record of the sales table. So for each item a only need the last sale.
How can i create such a query?
Thanks for your help.
-
Jul 22nd, 2007, 12:17 PM
#2
Fanatic Member
Re: Help With SQL Query
something like this?
SELECT Inventory.Description, Max(Sales.DateSOld) AS MaxVanDateSOld, Max(Sales.ItemNUm) AS MaxVanItemNUm, Max(Sales.Customername) AS MaxVanCustomername
FROM Inventory INNER JOIN Sales ON Inventory.ID = Sales.ItemNUm
GROUP BY Inventory.Description;
I've created this query on a MS Access DB. Because you didn't mention wich type you were using. I think it should do the trick.
Next time ask databasequestions here
- Use the thread tools to Mark your Thread as Resolved when your question is answered.
- Please Rate my answers if they where helpful.
-
Jul 22nd, 2007, 12:20 PM
#3
Re: Help With SQL Query
This will do it:
Code:
Select * From SomeTable S1
Where S1.DateSold=(Select Max(S2.DateSold) From SomeTAble S2
Where S2.ItemNum=S1.ItemNum)
Basically only selecting the rows where the DateSold is the max DateSold for that item.
-
Jul 22nd, 2007, 01:12 PM
#4
Thread Starter
Frenzied Member
Re: Help With SQL Query
Thanks all
robbedaya:
the problem is it returns the biggest customer name Max(customername) even if it isn't the same as the max(datesold)
szlamany:
What happens when two people ordered the same day? I only want one of them returned.
I am using MS Access.
-
Jul 22nd, 2007, 01:16 PM
#5
Re: Help With SQL Query
 Originally Posted by shragel
szlamany:
What happens when two people ordered the same day? I only want one of them returned.
Well - tell me which one you want returned if two customers order.
Does anything indicate the "latest" one in the day? Do you simply want the "latest" alphabetical one?
-
Jul 22nd, 2007, 01:17 PM
#6
Thread Starter
Frenzied Member
Re: Help With SQL Query
The "latest" alphabetical one?
-
Jul 22nd, 2007, 01:32 PM
#7
Re: Help With SQL Query
Well - that does make it a bit more complex.
Maybe it's best to create two views so that it's easier to see how this is going to work. You could always put the two views into "derived" tables like my first example - but basically it's going to work the same either way.
These two views give you the hook to the row wanted.
Code:
Create View MaxDateSold
As Select ItemNum,Max(DateSold) "MaxDateSold"
From SomeTable
Create View LastDateCust
As Select MD.ItemNum,MD.MaxDateSold,Max(S1.CustomerName)
From MaxDateSold MD
Left Join SomeTable S1 on S1.ItemNum=MD.ItemNum
and S1.DateSold=MD.MaxDateSold
The second view - LastDateCust - should be what you JOIN to first.
Code:
Select * From ...
Left Join LastDateCust LD on LD.ItemNum=... and LD.MaxDateSold=... and LD.CustomerName=...
Left Join ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|