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

Query Help

Status
Not open for further replies.

ToneSta

Technical User
Jun 17, 2003
5
AU
I have some data from a table "unitp" as below:

Code Price Date
---- ----- ----
ABC 12.04 30/11/02
ABC 12.05 31/12/02
DEF 5.06 31/05/02
DEF 8.03 30/06/02
DEF 2.45 31/07/02

i want to able to run a query that will output the record with the maximum Date showing the Code and Price, ie:

Code Price
---- -----
ABC 12.05
DEF 2.45

I have only been able to get this far, which doesn't really get what i want:

SELECT code, price, date
FROM unitp
WHERE code = 'ABC'
AND date = (SELECT MAX(date)
FROM unitp
WHERE code = 'ABC')

Would anyone know what would be the best way?

Many Many Thanks

 
SELECT code, price, date
FROM unitp
where date in (SELECT MAX(date)
FROM unitp unitp2
WHERE unitp.code = unitp2.code)

this assumes that each code can have only one record per date
 
Hi,

I think this should work..

SELECT code,price,date from
unitp P
INNER JOIN
(SELECT code, Max(date) dte
FROM unitp
Group by code) TBL ON TBL.Code=P.Code and TBL.dte= p.dte

Hope it helps...


Sunil
 
Thanks guys ....fluteplr, yours did the trick. Couldn't get sunila7's working ...might be syntax or something. i'm using sql 8. hmmmm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top