tb表
字段
create table tb
(id int identity(1,1) primary key,
xingming varchar(10),
riqi datetime,
iflag int)
现有记录
insert into tb
select '张三','2011-03-01',1 union all
select '张三','2011-03-02',2 union all
select '张三','2011-03-03',0 union all
select '李四','2011-03-01',1 union all
select '李四','2011-03-01',2 union all
select '王五','2011-03-01',1
条件
1、按名字分类,不管日期如何,只要iflag有一条记录是0的,则不统计
2、如果有多条算一条
分析
按1规则,张三有一条记录是0
按2规则,李四和王五虽然有多条,但是都没有iflag=0的情况,各算1条
结果
2
按照国际管理,感谢高人,热心人,好人,路人~

解决方案 »

  1.   

    create table tb
    (id int identity(1,1) primary key,
    xingming varchar(10),
    riqi datetime,
    iflag int)
    insert into tb
    select '张三','2011-03-01',1 union all
    select '张三','2011-03-02',2 union all
    select '张三','2011-03-03',0 union all
    select '李四','2011-03-01',1 union all
    select '李四','2011-03-01',2 union all
    select '王五','2011-03-01',1select xingming,COUNT(1) from tb a
    where not exists(select 1 from tb where xingming=a.xingming and iflag=0)
    group by xingming
    /*
    xingming   
    ---------- -----------
    李四         2
    王五         1
      

  2.   

    感谢楼上大侠,我期待的结果是一个数字:2
    而不是列出具体的明细select count(*)
    from
    (
    select count(*) as b
    from tb a
    where not exists
    (
    select * 
    from tb
    where a.xingming = xingming and iflag=0
    )
    group by xingming) b1
    刚才自己写了一下。不过感觉有点麻烦,期待高人能给出更简洁的
      

  3.   

    create table tb
    (id int identity(1,1) primary key,
    xingming varchar(10),
    riqi datetime,
    iflag int)
    insert into tb
    select '张三','2011-03-01',1 union all
    select '张三','2011-03-02',2 union all
    select '张三','2011-03-03',0 union all
    select '李四','2011-03-01',1 union all
    select '李四','2011-03-01',2 union all
    select '王五','2011-03-01',1select COUNT(distinct xingming) from tb a
    where not exists(select 1 from tb where xingming=a.xingming and iflag=0)/*-----------
    2
      

  4.   


    create table tb
    (id int identity(1,1) primary key,
    xingming varchar(10),
    riqi datetime,
    iflag int)
    insert into tb
    select '张三','2011-03-01',1 union all
    select '张三','2011-03-02',2 union all
    select '张三','2011-03-03',0 union all
    select '李四','2011-03-01',1 union all
    select '李四','2011-03-01',2 union all
    select '王五','2011-03-01',1select count(distinct xingming)
    from tb a
    where xingming not in (select xingming from tb where iflag=0)drop table tb/*2
      

  5.   


    create table tb
    (id int identity(1,1) primary key,
    xingming varchar(10),
    riqi datetime,
    iflag int)insert into tb
    select '张三','2011-03-01',1 union all
    select '张三','2011-03-02',2 union all
    select '张三','2011-03-03',0 union all
    select '李四','2011-03-01',1 union all
    select '李四','2011-03-01',2 union all
    select '王五','2011-03-01',1SELECT COUNT(DISTINCT xingming ) FROM TB 
    WHERE xingming NOT IN (SELECT xingming FROM TB WHERE iflag=0)/*
    -----------
    2(1 row(s) affected)
    */