表talbe 如下,五个字段(内容只为举例)
A   B   C   D   E
1   a   a   a   a
2   a   b   b   b
3   b   b   b   b
4       a   a   c
5       b   b   e
6       d   d   d结果要求:1.B字段为'a'或空
         2.C,D字段完全相同,若有多条,则取B不为空那条记录
           
举例结果如下:
A   B   C   D   E
1   a   a   a   a
2   a   b   b   b
6       d   d   d  

解决方案 »

  1.   

    --> 测试数据: #T
    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (A int,B varchar(11),C varchar(11),D varchar(11),E varchar(11))
    insert into #T
    select 1,'a','a','a','a' union all
    select 2,'a','b','b','b' union all
    select 3,'b','b','b','b' union all
    select 4,'','a','a','c' union all
    select 5,'','b','b','e' union all
    select 6,'','d','d','d'select * from #T as t where not exists (select 1 from #T where C=t.C and D=t.D and isnull(B,'')>t.isnull(B,''))/*
    A           B           C           D           E
    ----------- ----------- ----------- ----------- -----------
    1           a           a           a           a
    3           b           b           b           b
    6                       d           d           d
    */
      

  2.   

    A  B  C  D  E 
    1  a  a  a  a 
    2  a  b  b  b 
    6     d  d  d  是要这个结果,B='a' or B=''
      

  3.   

    --> 测试数据: #T
    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (A int,B varchar(11),C varchar(11),D varchar(11),E varchar(11))
    insert into #T
    select 1,'a','a','a','a' union all
    select 2,'a','b','b','b' union all
    select 3,'b','b','b','b' union all
    select 4,'','a','a','c' union all
    select 5,'','b','b','e' union all
    select 6,'','d','d','d'select * from #T as t where isnull(B,'') in ('a','') and not exists (select 1 from #T where isnull(B,'') in ('a','') and C=t.C and D=t.D and isnull(B,'')>isnull(t.B,''))/*
    A           B           C           D           E
    ----------- ----------- ----------- ----------- -----------
    1           a           a           a           a
    2           a           b           b           b
    6                       d           d           d
    */
      

  4.   

    isnull(B,'') in ('a','')---有点局限性,B='a' or B=''   也可能B='c' or B=''
                     也可能B='d' or B=''
                     。。
      

  5.   


    select *
    from table t
    where B in ('a','') and not exists (select * from table where C=t.C and D=t.D and isnull(B,'')>isnull(t.B,''))
      

  6.   


    select *
    from table t
    where B in('a','') and not exists (select * from table where isnull(B,'')>isnull(t.B,''))
      

  7.   

    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (A int,B varchar(11),C varchar(11),D varchar(11),E varchar(11))
    insert into #T
    select 1,'a','a','a','a' union all
    select 2,'a','b','b','b' union all
    select 3,'b','b','b','b' union all
    select 4,'','a','a','c' union all
    select 5,'','b','b','e' union all
    select 6,'','d','d','d'  union all 
    select 1,'a','a','a','a' union all
    select 2,'a','b','b','b' union all
    select 3,'b','b','b','b' union all
    select 4,'','a','a','c' union all
    select 5,'','b','b','e' union all
    select 6,'','d','d','d' union all 
    select 1,'a','c','d','a'
    select * from #Tselect * from #T as t where b in ('a','') and c=d and not exists (select 1 from #T where b in ('a','') and C=t.C and D=t.D and c=t.d and isnull(B,'')>isnull(t.B,''))
      result:
      A           B           C           D           E           
    ----------- ----------- ----------- ----------- ----------- 
    1           a           a           a           a
    2           a           b           b           b
    3           b           b           b           b
    4                       a           a           c
    5                       b           b           e
    6                       d           d           d
    1           a           a           a           a
    2           a           b           b           b
    3           b           b           b           b
    4                       a           a           c
    5                       b           b           e
    6                       d           d           d
    1           a           d           d           a
    1           a           c           d           a(所影响的行数为 14 行)A           B           C           D           E           
    ----------- ----------- ----------- ----------- ----------- 
    1           a           a           a           a
    2           a           b           b           b
    1           a           a           a           a
    2           a           b           b           b
    1           a           d           d           a(所影响的行数为 5 行)
      

  8.   

    select 1 from #T where b in ('a','') and C=t.C and D=t.D and c=t.d and isnull(B,'')>isnull(t.B,'')
    这句话是什么意思,我看不懂请大哥帮我下特别是最后后半句