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 @tbl1
values
('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 @tbl2
values
('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')[SOAPBOX]You should be supplying the above SQL scriptlet. It makes it easier for people to help you.[/SOAPBOX];with LatestHistory
as(
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.wt
from
@tbl1 today
inner join
LatestHistory history
on today.item = history.item
and today.seqno = history.seqno
=================================================
There is a foolish corner in the brain of the wisest man. -Aristotle, philosopher (384-322 BCE)