有一表,里面有DWDM,DM两个字段,我想查询这样的结果:
比如DWDM为5的,它的所有数据中的DM列的值都为'-',我把这个DWDM取出来。如数据:
DWDM  DM
1      5
1      -
2      -
2      -
3      -
3      s
3      -
4      -那我的结果是DWDM值为:2和4
谢谢!

解决方案 »

  1.   

    SELECT DISTINCT DWDM FROM TB WHERE DM=N'-'
      

  2.   

         select * from tb where DWDM=5 and DM='-'
      

  3.   

    select distinct dwdm from tb where dm='-'
      

  4.   

    SELECT DWDM FROM TB WHERE DM=N'-' 
    GROUP BY DM HAVING COUNT(DM)=
    (SELECT COUNT(DM) FROM TB GROUP BY DM WHERE DM=N'-')
      

  5.   


    select * from TB a where not exists(select 1 from TB where a.DWDM=DWDM and DM<>'-')
      

  6.   

    DWDM  DM select distinct(DWDM)from table a
    where not exists(select 1 from table where a.DWDM=DWDM and DM<>'-')
      

  7.   


    create table #T(DWDM int,DM varchar(50))
    insert into #T
    select 1      ,'5' union all
    select 1      ,'-' union all
    select 2      ,'-' union all
    select 2      ,'-' union all
    select 3      ,'-' union all
    select 3      ,'s' union all
    select 3      ,'-' union all
    select 4      ,'-' select distinct DWDM from #T a where not exists(select 1 from #T where a.DWDM=DWDM and DM<>'-')----------------------------------------
    2
    4
      

  8.   

    DECLARE @TB TABLE(DWDM INT, DM CHAR(1))
    INSERT @TB
    SELECT 1 ,     '5' UNION ALL
    SELECT 1 ,     '-' UNION ALL
    SELECT 2 ,     '-' UNION ALL
    SELECT 2 ,     '-' UNION ALL
    SELECT 3 ,     '-' UNION ALL
    SELECT 3 ,     's' UNION ALL
    SELECT 3 ,     '-' UNION ALL
    SELECT 4 ,     '-' SELECT DWDM FROM @TB T
    GROUP BY DWDM
    HAVING COUNT(DM)=(SELECT COUNT(DM) FROM @TB  WHERE DM='-' AND DWDM=T.DWDM GROUP BY DWDM)
    (所影响的行数为 8 行)DWDM        
    ----------- 
    2
    4(所影响的行数为 2 行)
    --SELECT COUNT(DM) FROM @TB  WHERE DM='-' GROUP BY DWDM
      

  9.   

    --注意“全部”二字
    drop table a;create table a(dwdm int, dm varchar(4));
    insert into a(dwdm, dm)
    select
    1,'5' union all select
    1,'-' union all select
    2,'-' union all select
    2,'-' union all select
    3,'-' union all select
    3,'s' union all select
    3,'-' union all select
    4,'-';select dwdm
    from a
    group by dwdm
    having count(dwdm)=sum(case dm when '-' then 1 else 0 end);
      

  10.   

            create table #tmp(DWDM int,DM varchar(8))
    insert into #tmp
    select  1,      '5' union all 
    select  1,      '-' union all 
    select  2,      '-' union all
    select  2,      '-' union all 
    select  3,      '-' union all 
    select  3,      's' union all 
    select  3,      '-' union all 
    select  4,      '-' 
    select distinct * from #tmp t where not exists(select 1 from #tmp where DWDM=t.DWDM  and DM<>'-')
      

  11.   


    create table tb20091029_1 (DWDM int,DM varchar(10))
    insert into tb20091029_1
    select '1','5'
    union all select '1','-'
    union all select '2','-'
    union all select '2','-'
    union all select '3','-'
    union all select '3','s'
    union all select '3','-'
    union all select '4','-'select distinct DWDM from tb20091029_1 t 
    where not exists (
        select 1 from tb20091029_1 where DWDM = t.DWDM and DM <> t.DM
    )
    ----------
    DWDM
    2
    4(2 件処理されました)----------
      

  12.   

    select distinct * from #tmp t where not exists(select 1 from #tmp where DWDM=t.DWDM  and DM <>'-')
      

  13.   

    drop table a;--------楼主:可以去测试一下:什么是效率!
    create table a(dwdm int, dm varchar(4));----循环插入8000000条记录行
    declare @a int
    set @a=1000000;
    while(@a>0)
    begin
    insert into a(dwdm, dm)
    select
    1,'5' union all select
    1,'-' union all select
    2,'-' union all select
    2,'-' union all select
    3,'-' union all select
    3,'s' union all select
    3,'-' union all select
    4,'-';
    set @a=@a-1
    end;--------8秒钟出数据
    select dwdm
    from a
    group by dwdm
    having count(dwdm)=sum(case dm when '-' then 1 else 0 end);--------10分钟了,还没出数据
    select distinct DWDM from a t 
    where not exists (
        select 1 from a where DWDM = t.DWDM and DM <> t.DM
    )
      

  14.   

    select dwdm,dm from tab1 where dm='5' and dm='-'