A IDNOAll VALID
001,002,003 0
001,002,003 0
003,004,005 1
001,007,009 1
001,008,003 1
001,004,0010 1
002,003,0010 1
001,002,0011 1
001,002,003 1B
IDNo  
001
002只要A表的IDNOAll中存在B的IDNo编号,并且A表的VALID是1的,那么就要把此类数据过滤掉
即得到AIDNOAll VALID
001,002,003 0
001,002,003 0
003,004,005 1

解决方案 »

  1.   


    declare @a table (id int,IDNOAll varchar(15),VALID int)
    insert into @a
    select 1,'001,002,003',0 union all
    select 2,'001,002,003',0 union all
    select 3,'003,004,005',1 union all
    select 4,'001,007,009',1 union all
    select 5,'001,008,003',1 union all
    select 6,'001,004,0010',1 union all
    select 7,'002,003,0010',1 union all
    select 8,'001,002,0011',1 union all
    select 9,'001,002,003',1declare @B table (IDNo varchar(3))
    insert into @B
    select '001' union all
    select '002'select d.* from @a d left join
    (
    select  id from @a a cross join @B b 
    where a.VALID=0 or charindex(','+b.IDNo+',',','+a.IDNOAll)=0
    group by id having (count(1)>1)
    )c 
    on d.id=c.id where c.id is not null/*
    id          IDNOAll         VALID
    ----------- --------------- -----------
    1           001,002,003     0
    2           001,002,003     0
    3           003,004,005     1
    */
      

  2.   

    declare @a table (IDNOAll varchar(15),VALID int)
    insert into @a
    select '001,002,003',0 union all
    select '001,002,003',0 union all
    select '003,004,005',1 union all
    select '001,007,009',1 union all
    select '001,008,003',1 union all
    select '001,004,0010',1 union all
    select '002,003,0010',1 union all
    select '001,002,0011',1 union all
    select '001,002,003',1declare @B table (IDNo varchar(3))
    insert into @B
    select '001' union all
    select '002'
    select a.* from @a A where not exists(select 1 from @b B where charindex(','+b.IDNo+',',','+a.IDNOAll+',') > 0 and a.VALID = 1)/*
    IDNOAll         VALID       
    --------------- ----------- 
    001,002,003     0
    001,002,003     0
    003,004,005     1(所影响的行数为 3 行)
    */
      

  3.   


    是的,所以处理起来比较麻烦,但是你写的这个效率不是很好啊,是否有更好的呢?
    select a.id INTO #T from @a a
    INNER JOIN @b b ON a.VALID = 1 and PATINDEX('%'+ b.IDNo + '%', a.IDNOAll) >0 
    select * from  @a where id not in(select * from #T)
    drop table #T
    但是这个效率也很低,因为数据量比较大啊
    PATINDEX又不能索引。请大家再看看