Subquery , get latest price (1 Viewer)

selvsagt

Registered User.
Local time
Today, 22:57
Joined
Jun 29, 2006
Messages
99
Probably a simple question, but I cant get my mind to the logics here.. :banghead:
I have lots of products with changing prices. I need to get a list of all products with the latest price per product. I've done this in access with nested queries, but never in SQL before. I need a subquery (i think), and have been playing around for some time now.

These are the tables involved:

tblProd | product table, produkt is the productname
tblKurs | price table, NAV is the pricem and kursdato is the price date.

This is sort of what i want;

tblProd.produkt SisteKurs
"product 1" 10
"product 2" 8
"product 3" 7
"product 4" 15

This is what I have so far, but this only returns the latest price for product1 (not the rest of the products).

SELECT dbo.tblProd.Produkt, dbo.tblKurs.NAV AS Sistekurs, dbo.tblKurs.Kursdato
FROM dbo.tblProd INNER JOIN
dbo.tblKurs ON dbo.tblProd.prodID = dbo.tblKurs.prodID
WHERE (dbo.tblKurs.Kursdato =
(SELECT MAX(Kursdato) AS xkursdato
FROM dbo.tblKurs AS tblKurs_x))
 

CJ_London

Super Moderator
Staff member
Local time
Today, 21:57
Joined
Feb 19, 2013
Messages
16,666
try

Code:
 SELECT dbo.tblProd.Produkt, dbo.tblKurs.NAV AS Sistekurs, dbo.tblKurs.Kursdato
FROM dbo.tblProd INNER JOIN dbo.tblKurs ON dbo.tblProd.prodID = dbo.tblKurs.prodID
WHERE (dbo.tblKurs.Kursdato =(SELECT MAX(Kursdato) AS xkursdato
FROM dbo.tblKurs AS tblKurs_x [COLOR=red]WHERE prodid= dbo.tblProd.prodID[/COLOR] ))
 

selvsagt

Registered User.
Local time
Today, 22:57
Joined
Jun 29, 2006
Messages
99
Thank you !!!!!!!!!!!!!!!!!!!!!
 

Users who are viewing this thread

Top Bottom