Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2012-06-01 : 12:19:26
|
[CODE]declare @tbl1 table ( item varchar(10), wt varchar(10), seqno int, [date] date )declare @tbl2 table ( item varchar(10), wt varchar(10), seqno int, [date] date )insert into @tbl1values ('item1', 'wt1', 1, '5/30/2012'), ('item2', 'wt2', 1, '5/30/2012'), ('item3', 'wt3', 1, '5/30/2012'), ('item4', 'wt4', 2, '5/30/2012'), ('item5', 'wt5', 2, '5/30/2012'), ('item6', 'wt6', 1, '5/30/2012')insert into @tbl2values ('item1', 'wt1', 1, '5/29/2012'), ('item2', 'wt2', 1, '5/29/2012'), ('item3', 'wt3', 1, '5/29/2012'), ('item4', 'wt4', 2, '5/29/2012'), ('item5', 'wt5', 2, '5/29/2012'), ('item6', 'wt6', 1, '5/29/2012'), ('item1', 'wt1', 1, '5/28/2012'), ('item2', 'wt2', 1, '5/28/2012'), ('item3', 'wt3', 1, '5/28/2012'), ('item4', 'wt4', 2, '5/28/2012'), ('item5', 'wt5', 2, '5/28/2012'), ('item6', 'wt6', 1, '5/28/2012'), ('item1', 'wt1', 1, '5/27/2012'), ('item2', 'wt2', 1, '5/27/2012'), ('item3', 'wt3', 1, '5/27/2012'), ('item4', 'wt4', 2, '5/27/2012'), ('item5', 'wt5', 2, '5/27/2012'), ('item6', 'wt6', 1, '5/27/2012')[/CODE][SOAPBOX]You should be supplying the above SQL scriptlet. It makes it easier for people to help you.[/SOAPBOX][CODE];with LatestHistoryas( select a.item, a.wt, a.seqno from ( select item, wt, seqno, row_number() over(partition by item, seqno order by [date] DESC) rn from @tbl2 ) a where a.rn = 1 )select today.item, today.wt, today.seqno, history.wtfrom @tbl1 todayinner join LatestHistory history on today.item = history.item and today.seqno = history.seqno[/CODE]=================================================There is a foolish corner in the brain of the wisest man. -Aristotle, philosopher (384-322 BCE) |
 |
|