27,579
社区成员
发帖
与我相关
我的任务
分享
create table #ls(barcode varchar(30),vendorid char(6),indate datetime,price float)
insert into #ls(barcode,vendorid,indate,price)
select '11','01','2009-01-01',0.1 union all
select '11','02','2009-02-01',0.7 union all
select '11','03','2009-02-03',0.9 union all
select '11','01','2009-05-01',0.2 union all
select '11','02','2009-06-01',1.4 union all
select '11','03','2009-07-03',1.8 union all
select '21','01','2009-01-01',0.1 union all
select '21','02','2009-02-01',0.7 union all
select '21','03','2009-02-03',0.9 union all
select '21','01','2009-05-01',0.3 union all
select '21','02','2009-06-01',2.1 union all
select '21','03','2009-07-03',2.7
select b.barcode,b.vendorid,b.price from (select barcode,vendorid,max(indate) as indate from #ls group by barcode,vendorid) a
left join #ls b on a.barcode=b.barcode and a.vendorid=b.vendorid and a.indate=b.indate
select * from #ls t
where INDAETE=(select MAX(INDATER) from #ls where barcode=t.barcode and vendorid=t.vendorid )
select t.barcode,t.vendorid,t.price
from #ls t
where not exists(select 1 from #ls where
barcode=t.barcode and vendorid=t.vendorid and indate>t.indate)
--try
select * from #ls t
where not exists(select 1 from #ls where barcode=t.barcode and vendorid=t.vendorid
and indate>t.indate)