日期:2014-05-17 浏览次数:20603 次
drop table a
drop table b
go
create table A
(
TemplateId varchar(10),
asxcode varchar(10),
[Date] datetime,
ReportId int,
Code varchar(20),
ItemID int,
Value int
)
insert into a
select 'P' ,'AAD', '2013/6/30', 1,'BS00000300',111,100 union all
select 'P' ,'AAD', '1999/12/30', 2,'BS00000400',222,200 union all
select 'P' ,'AAD', '2000/6/30', 3,'BS00000400',333,300 union all
select 'N' ,'ABN', '2010/12/30', 4,'BS00002645',444,400 union all
select 'I' ,'ADX', '2012/6/30', 5,'CF00001300',555,500
create table b(ItemID int,TemplateId varchar(20))
insert into b
select 111, 'NBIPV' union all
select 222, 'NBIP' union all
select 333, 'BI' union all
select 444, 'N' union all
select 555, 'PVN'
select case when exists(select 1 from b
where charindex(a.TemplateId,b.TemplateId)>0
and b.ItemID = a.ItemID)
then 'Y'
else 'N'
end as mark,
a.*
from a
/*
mark TemplateId asxcode Date ReportId Code