January 27, 2008 at 3:18 am
Hi
I am just starting out on sql programming.
I have one table with three columns
Item Name | Price |Date
Item1 | 44 |25-Jan-08
Item1 | 45 |26-Jan-08
Item1 | 43 |27-Jan-08
Item2 | 21 |26-Jan-08
Item2 | 34 |28-Jan-08
Item3 | 91 |27-Jan-08
Item3 | 98 |29-Jan-08
I want to pick latest price of all items and produce a result as below
Item1 | 43 |27-Jan-08
Item2 | 34 |28-Jan-08
Item3 | 98 |29-Jan-08
Please help me on this
Thank You
-Jag
January 27, 2008 at 4:42 am
Please review the article "Please provide DDL and sample data" at
http://www.aspfaq.com/etiquette.asp?id=5006
As a guess:
Create table ItemPrices
(ItemName varchar(255) not null
,EffectiveDate datetime not null
,PriceAmt numeric(8,2) not null
,constraint ItemPrices_PK primary key (ItemName , EffectiveDate )
)
go
select ItemPrices.*
from ItemPrices
join (select ItemName
, MAX(EffectiveDate ) as HighestEffectiveDate
from ItemPrices
group by ItemName
) as ItemPrices_Lastest
on ItemPrices_Lastest.ItemName = ItemPrices.ItemName
and ItemPrices_Lastest.HighestEffectiveDate = ItemPrices.EffectiveDate
SQL = Scarcely Qualifies as a Language
January 27, 2008 at 6:23 am
Excellent. It worked for me.
Thanks a lot for your kind help
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply