本帖最后由 cqf254112311 于 2012-01-11 16:17:32 编辑

解决方案 »

  1.   

    select *
    from tb t
    where exists(select 1 from tb where 单据号=t.单据号 and (科目号='A' or 科目号='B'))
      

  2.   


    declare @T table (单据号 varchar(2),行号 int,科目号 varchar(1))
    insert into @T
    select '01',1,'A' union all
    select '01',2,'B' union all
    select '02',1,'A' union all
    select '02',2,'C' union all
    select '02',3,'D' union all
    select '03',1,'C' union all
    select '04',1,'B' union all
    select '04',2,'E'select distinct a.* from @t a left join @t b 
    on a.单据号=b.单据号 where b.科目号='a' or b.科目号='b'
    /*
    单据号  行号          科目号
    ---- ----------- ----
    01   1           A
    01   2           B
    02   1           A
    02   2           C
    02   3           D
    04   1           B
    04   2           E
    */
      

  3.   

    create table tb
    (
    单据号 int,
    行号 int,
    科目号 nvarchar(1)
    )
    insert into tb values(01,1,'A')
    insert into tb values(01,1,'B')
    insert into tb values(02,1,'A')
    insert into tb values(02,1,'C')
    insert into tb values(02,1,'D')
    insert into tb values(03,1,'C')
    insert into tb values(04,1,'B')
    insert into tb values(04,1,'E')
    select * from tb 
    where 单据号 in(select 单据号 from tb where 科目号 in('A','B') group by 单据号)
    /*
    单据号,行号,科目号
    1,1,A
    1,1,B
    2,1,A
    2,1,C
    2,1,D
    4,1,B
    4,1,E(7 行受影响)