表 结构  id code  type   依次是number,varchar,number类型
如何 查出  code 一样 type 为 1 的 记录 大于2 
比如  1, 003  ,1
      2, 003  ,1
      3, 004,2
      4, 005,3
      5, 006,1    
      6, 006,1 这里 应该 是 003
             006

解决方案 »

  1.   

    select id,code
    from  tab
    where type='1'
    group by id
    having count(*)>=2
      

  2.   

    select id,code
    from  tab
    where type='1'
    group by code
    having count(*)>2这样。。
      

  3.   


    with tb as(
    select 1 id,'003' code,1 type from dual union all
    select 2,'003',1 from dual union all
    select 3,'004',2 from dual union all
    select 4,'005',3 from dual union all
    select 5,'006',1 from dual union all
    select 6,'006',1 from dual)
    --以上为提供数据的语句
    select code
    from (select code,type,count(id) cnt from tb group by code,type)
    where cnt>=2COD
    ---
    003
    006
      

  4.   


    --偷学下杨哥的
    已写入 file afiedt.buf  1  with tb as(
      2  select 1 id,'003' code,1 type from dual union all
      3  select 2,'003',1 from dual union all
      4  select 3,'004',2 from dual union all
      5  select 4,'005',3 from dual union all
      6  select 5,'006',1 from dual union all
      7  select 6,'006',1 from dual)
      8  select code
      9  from  tb
     10  where type=1
     11  group by code,type
     12* having count(id)>=2
    SQL> /COD
    ---
    003
    006
      

  5.   

    select distinct code
    from tb a
    where a.type=1 and (select count(id) from tb b where a.type=b.type)>2