我有一张表test表,表中的字段有
ID varchar2(20)        
status         number(20) 
今表中的数据有:
ID      STATUS  
1 2
1 2
2 1
2 2
3 0
4 1
4 1
4 1
ID表示记录ID(该字段不是主键)
STATUS表示状态(该字段的取值为0,1,2)
注:该表中没有主键。
我要的效果是:
如果ID相同,并且STATUS所对应的值全为1,则表示“完成”,并把STATUS字段设置为1;
如果ID相同,并且STATUS所对应的值全为2,则表示“失败”,并把STATUS字段设置为2;
如果ID相同,并且STATUS所对应的值不一样,并且非上述情况,则表示“处理中”,并把STATUS字段设置为0;如:
ID STATUS
1 2
2 0
3 0
4 1谢谢大家!

解决方案 »

  1.   

    看看这个吧
    很相似
    http://topic.csdn.net/u/20090822/13/cfa0211f-6073-443e-9922-e3d0fca6ca8b.html
      

  2.   


    select id,
      case when max(status)=1 and min(status)=1 then 1
      when min(status)=2 then 2 else 0 end status
    from test
    group by id
      

  3.   

    update test set status=0 from test t1
    where exists(select 1 from test t2 where t1.id=t2.id and t1.status<>t2.status)go
    select distinct * from test
    /*
    ID          STATUS      
    ----------- ----------- 
    1           2
    2           0
    3           0
    4           1
    */
      

  4.   

    select id,
      case when max(status)=1 and min(status)=1 then 1
      when min(status)=2 then 2 else 0 end status
    from test
    group by id order by id