本帖最后由 iidpj 于 2012-02-09 18:39:07 编辑

解决方案 »

  1.   

       
       select * from csdn where factoryid = 11102 
       union 
        select customerid,customername,factoryid from (select * from csdn a where 
        customerid not in 
       (select distinct customerid from csdn where factoryid=11102) order by factoryid desc)  where rownum<= (select count(distinct(customerid)) from csdn a where customerid not in (select distinct customerid from csdn where factoryid=11102));    
      

  2.   

      将11102换成 你需要的数字   其实用过程或者函数比较简单 如果要sql 可能比较麻烦  给出的方法肯定不是最好的   但是貌似可以满足你的要求
      

  3.   

       对了 将表csdn 换成你的表cus_factory
      

  4.   

    select * from cus_factory where id in(
    --当@factoryid 不等于 facotryid 时 取factoryid为大的记录
    select max(id)keep(dense_rank first order by factoryid desc)
    from cus_factory  
    group by customerid 
    having customerid not in (select distinct customerid from cus_factory where factoryid=@factoryid)
    union
    --带入@factoryid为参数 同一customerid有且只有一条记录 
    select min(id)keep(dense_rank first order by factoryid desc)
    from cus_factory where factoryid=@factoryid group by customerid
    )