关于表查询的问题

duqiangcise 2009-10-09 06:44:31
我有一张表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

谢谢大家!
...全文
99 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
oraclemch 2009-10-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wildwave 的回复:]
SQL codeselect id,casewhenmax(status)=1andmin(status)=1then1whenmin(status)=2then2else0end statusfrom testgroupby id
[/Quote]

支持,很对!
jiaruimin11 2009-10-09
  • 打赏
  • 举报
回复
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
bancxc 2009-10-09
  • 打赏
  • 举报
回复
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
*/
小灰狼W 2009-10-09
  • 打赏
  • 举报
回复

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
小灰狼W 2009-10-09
  • 打赏
  • 举报
回复
看看这个吧
很相似
http://topic.csdn.net/u/20090822/13/cfa0211f-6073-443e-9922-e3d0fca6ca8b.html

17,082

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧