我现在有一如下字段:
XM01(姓名)     GZ01(工作序号)   BS01(标识是否工作)
张三               00001              Y
张三               00002              Y
李四               00001              N
张三               00003              N
李四               00002              Y
王五               00001              N
王五               00002              N
我要查看王五或其他人(XM001) 这个人 所有的工作序号 (00001、00002一直到00020),的标识(BS01)是否有都为N~!!!

解决方案 »

  1.   


    ---------------------------------
    --  Author: htl258(Tony)
    --  Date  : 2009-07-20 08:47:53
    ---------------------------------
    --> 生成测试数据表:tbIf not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([XM01(姓名)] nvarchar(2),[GZ01(工作序号)] nvarchar(6),[BS01(标识是否工作)] nvarchar(1))
    Insert tb
    Select '张三','00001','Y' union all
    Select '张三','00002','Y' union all
    Select '李四','00001','N' union all
    Select '张三','00003','N' union all
    Select '李四','00002','Y' union all
    Select '王五','00001','N' union all
    Select '王五','00002','N'
    Go
    --Select * from tb-->SQL查询如下:
    select *
    from tb t 
    where not exists(
    select 1
    from tb
    where [XM01(姓名)]=t.[XM01(姓名)]
      and [BS01(标识是否工作)]<>t.[BS01(标识是否工作)]
      and [GZ01(工作序号)] between '00001' and '00020')
      and [GZ01(工作序号)] between '00001' and '00020'
    /*
    XM01(姓名) GZ01(工作序号) BS01(标识是否工作)
    -------- ---------- ------------
    王五       00001      N
    王五       00002      N(2 行受影响)
    */
      

  2.   

    SELECT COUNT(DISTINCT GZ01) FROM (SELECT GZ01 FROM TB WHERE XM01='王五' AND BS01='N') AS T GROUP BY GZ01 HAVING COUNT(DISTINCT GZ01)=20
      

  3.   


    ---------------------------------
    --  Author: htl258(Tony)
    --  Date  : 2009-07-20 08:47:53
    ---------------------------------
    --> 生成测试数据表:tbIf not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([XM01(姓名)] nvarchar(2),[GZ01(工作序号)] nvarchar(6),[BS01(标识是否工作)] nvarchar(1))
    Insert tb
    Select '张三','00001','Y' union all
    Select '张三','00002','Y' union all
    Select '李四','00001','N' union all
    Select '张三','00003','N' union all
    Select '李四','00002','Y' union all
    Select '王五','00001','N' union all
    Select '王五','00002','N'
    Go
    --Select * from tb-->SQL查询如下:
    select *
    from tb t 
    where not exists(
    select 1
    from tb
    where [XM01(姓名)]=t.[XM01(姓名)]
      and [BS01(标识是否工作)]<>t.[BS01(标识是否工作)]
      and [GZ01(工作序号)] between '00001' and '00020')
      and [GZ01(工作序号)] between '00001' and '00020'
    /*
    XM01(姓名) GZ01(工作序号) BS01(标识是否工作)
    -------- ---------- ------------
    王五       00001      N
    王五       00002      N(2 行受影响)
    */
      

  4.   


    ---------------------------------
    --  Author: htl258(Tony)
    --  Date  : 2009-07-20 08:47:53
    ---------------------------------
    --> 生成测试数据表:tbIf not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([XM01(姓名)] nvarchar(2),[GZ01(工作序号)] nvarchar(6),[BS01(标识是否工作)] nvarchar(1))
    Insert tb
    Select '张三','00001','Y' union all
    Select '张三','00002','Y' union all
    Select '李四','00001','N' union all
    Select '张三','00003','N' union all
    Select '李四','00002','Y' union all
    Select '王五','00001','N' union all
    Select '王五','00002','N'
    Go
    --Select * from tb-->SQL查询如下:
    select *
    from tb t 
    where not exists(
    select 1
    from tb
    where [XM01(姓名)]=t.[XM01(姓名)]
      and [BS01(标识是否工作)]<>t.[BS01(标识是否工作)]
      and [GZ01(工作序号)] between '00001' and '00020')
      and [GZ01(工作序号)] between '00001' and '00020'
    /*
    XM01(姓名) GZ01(工作序号) BS01(标识是否工作)
    -------- ---------- ------------
    王五       00001      N
    王五       00002      N(2 行受影响)
    */
      

  5.   

    If not object_id('[tb]') is null
        Drop table [tb]
    Go
    Create table [tb]([XM01] nvarchar(2),[GZ01] nvarchar(6),[BS01] nvarchar(1))
    Insert tb
    Select '张三','00001','Y' union all
    Select '张三','00002','Y' union all
    Select '李四','00001','N' union all
    Select '张三','00003','N' union all
    Select '李四','00002','Y' union all
    Select '王五','00001','N' union all
    Select '王五','00002','N'
    Go--找出是BS001全部是N的--->对全部的BS01,没有一个是Yselect * from tb t where not exists (select * from tb where BS01='Y' and  XM01=t.XM01)
    /*
    XM01 GZ01   BS01
    ---- ------ ----
    王五   00001  N
    王五   00002  N(2 行受影响)*/
      

  6.   

     
    If not object_id('[tb]') is null
        Drop table [tb]
    Go
    Create table [tb]([XM01(姓名)] nvarchar(2),[GZ01(工作序号)] nvarchar(6),[BS01(标识是否工作)] nvarchar(1))
    Insert tb
    Select '张三','00001','Y' union all
    Select '张三','00002','Y' union all
    Select '李四','00001','N' union all
    Select '张三','00003','N' union all
    Select '李四','00002','Y' union all
    Select '王五','00001','N' union all
    Select '王五','00002','N'
    Go--有记录则不是都为N
    select * from tb where XM001='王五' and BS01='Y'