Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Jawad Khan
Starting Member
21 Posts |
Posted - 2008-10-17 : 02:19:58
|
| Hi,I have got two tables. Items And ItemsRateHistoryItems have two columsItemID and ItemNameItemsRateHistory has three columnsItemID, ChangeDate, NewRateHere is some sample data in ItemsRateHistory1------1-oct-08------$102------1-oct-08------$81------5-oct-08------$131------8-oct-08------$15Now I want to query this table to get the latest rates of Items. The output should be:1------8-oct-08------$152------1-oct-08------$8Can any one help me writing the query?Thanks |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-17 : 02:38:08
|
| select ItemId, ChangeDate, NewRatefrom(select ItemId, ChangeDate, NewRate, row_number() over (partition by ItemId order by ChangeDate desc) as num from ItemsRateHistory)twhere num=1Planning replaces chance by mistake |
 |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-10-17 : 02:45:07
|
| Declare @T table( id int, date datetime, price varchar(10))Insert into @TSelect 1,getdate()-5,'$10' union allSelect 1,getdate()-2,'$1' union allSelect 2,getdate(),'$8' union allSelect 1,getdate(),'$15'Select * from( select id,date, price, row_number() over( partition by id order by date) asRID from @T) T where RID=1 |
 |
|
|
Jawad Khan
Starting Member
21 Posts |
Posted - 2008-10-17 : 02:58:10
|
| Thanks guys. That was quick. I was not familiar to the row_number() and partition. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
Jawad Khan
Starting Member
21 Posts |
Posted - 2008-10-17 : 06:25:40
|
| Thanks Madhivanan, that was very helpful. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-17 : 08:27:06
|
quote: Originally posted by Jawad Khan Thanks Madhivanan, that was very helpful.
You are welcome MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|