I have been trying to figure out how to pull data out of sql table that holds the history of price changes for items in inventory. Example of how the data is laid out is below:
Item No Date changed Price
1 11/20/2012 15
2 11/28/2012 25
1 12/1/2012 18
I am needing to pull the last entry for each item no so that i can find where prices have change by more than a certain percent. Once I get the information all worked out but i am not sure how to pull only the last two updates for each item. Any help will be greatly appreciated
This is how i got it to work.
create table ##inventoryitems (ID INT Identity(1,1),itemno char(11));
insert into ##inventoryitems
select distinct itemno
from InventoryActPkgCostHist
where LocID=3
create table ##temptable (item# char(11),changedate datetime,price numeric(8,2));
DECLARE @Counter INT
SET @Counter = 1
while @Counter<=(select COUNT(*) from ##inventoryitems)
begin
insert into ##temptable
select top 2 i.itemno,i.ActPkgCostDate,i.ActPkgCost
from InventoryActPkgCostHist i join ##inventoryitesm temp on i.itemno= temp.itemno
where temp.id=@Counter and locidd=3
order by itemno,ActPkgCostDate desc
set @Counter=@Counter+1
end
Thank you for everyone’s help