比如表A有字段  单号 和 是否合格(0,1)
比如数据   
001,0
001,1
001,0
002,1
002,1
003,0
003,1我要找出都合格的单号。请问怎么写呢

解决方案 »

  1.   

    select 单号 from A where 是否合格=1
      

  2.   


    select 单号 from A where 是否合格=0
      

  3.   

    declare @tb table( billno varchar(32),flag bit)
    insert into @tb select '001',0 union all
    select '001',1 union all
    select '002',1 union all
    select '002',1 union all
    select '003',1 union all
    select '003',0select billno from @tb except select billno from @tb where flag=0/*
    billno
    002
    */
      

  4.   

    楼上的是SQL哪个版本?
    我这里用的2000,显示语法错误。
      

  5.   

    declare @tb table( billno varchar(32),flag bit)
    insert into @tb select '001',0 union all
    select '001',1 union all
    select '002',1 union all
    select '002',1 union all
    select '003',1 union all
    select '003',0select DISTINCT billno from @tb WHERE billno NOT IN ( select billno from @tb where flag=0)/*
    billno
    002
    */---SQL 2000  上面的应该可以用
      

  6.   


    --数据量超过10万级以上使用下面的写法
    declare @tb table( billno varchar(32),flag bit)
    insert into @tb select '001',0 union all
    select '001',1 union all
    select '002',1 union all
    select '002',1 union all
    select '003',1 union all
    select '003',0select DISTINCT billno from @tb a WHERE  NOT exists ( select billno from @tb where billno =a.billno and flag=0)/*
    billno
    002
    */
      

  7.   

    create table t1
    (
    col1 varchar(3),
    col2 int
    )
    insert into t1
    select '001',0 union all
    select '001',1 union all
    select '001',0 union all
    select '002',1 union all
    select '002',1 union all
    select '003',0 union all
    select '003',1
    select * from t1select col1,SUM(col2) as col2 from t1 group by col1 having COUNT(col1)=SUM(col2)------------------------
    col1 col2
    002 2MSSQL2000及以上版本都可以。