题目大概是这样的:某表:
tcode tname ttype tnum
1 A 1 100
1 A 1 500
1 B 1 200
1 B 2 600
2 A 3 400
2 A 3 700
2 B 1 400想要获取这样的结果:
tcode tname tnum tstatus
1 A 600 未完成
1 B 800 完成中
2 A 1100 完成
2 B 400 未完成tnum结果分组相加
注:分组中ttype若全为1,则tstatus为未完成
若含有一个2,则tstatus为完成中
若全为3,则tstatus为完成回来后想了一会,用下列语句得出:
select tcode,
tname,
tnum,
case
when tid = 1 then
'未完成'
when tid = 3 then
'完成'
when tid like '%2%' then
'完成中'
else
'异常'
end tstatus
from (select tcode,
tname,
sum(tnum) tnum,
max(decode(ttype, 1, ttype, null)) ||
max(decode(ttype, 2, ttype, null)) ||
max(decode(ttype, 3, ttype, null)) tid
from t_test
group by tcode, tname
order by tcode, tname);感觉语句有些繁琐,不知道是否有其他简易点的方法?
tcode tname ttype tnum
1 A 1 100
1 A 1 500
1 B 1 200
1 B 2 600
2 A 3 400
2 A 3 700
2 B 1 400想要获取这样的结果:
tcode tname tnum tstatus
1 A 600 未完成
1 B 800 完成中
2 A 1100 完成
2 B 400 未完成tnum结果分组相加
注:分组中ttype若全为1,则tstatus为未完成
若含有一个2,则tstatus为完成中
若全为3,则tstatus为完成回来后想了一会,用下列语句得出:
select tcode,
tname,
tnum,
case
when tid = 1 then
'未完成'
when tid = 3 then
'完成'
when tid like '%2%' then
'完成中'
else
'异常'
end tstatus
from (select tcode,
tname,
sum(tnum) tnum,
max(decode(ttype, 1, ttype, null)) ||
max(decode(ttype, 2, ttype, null)) ||
max(decode(ttype, 3, ttype, null)) tid
from t_test
group by tcode, tname
order by tcode, tname);感觉语句有些繁琐,不知道是否有其他简易点的方法?
解决方案 »
- 创建索引占用的空间大小计算
- 关于ORACLE高级复制中物化视图类型的选择.
- 关于oracle锁表的问题
- 缺失右括号的问题??
- 关于连接ORACLE数据库问题
- 有控制文件,日志文件,数据文件,请问如何恢复oracle数据库.
- oracle 8.0.5 for nt ,装完以后NET8 EASY CONFIG不能用,怎么办?
- 查询oracle数据库(调用存储过程)用多表连接性能好还是把单个表查询结果放在游标中一个个比较性能好(数据库很大)?多谢。
- 存储过程老出错!请高手指点
- Oracle 8i安装好之后,默认Enterprise Manager 登陆账号和密码是什么?
- Sql
- record is locked by another user
sum(ttype)=0 ,未完成
sum(ttype)=count(*) 已完成
两者之外就算完成中
tname,
decode(ttype,
1,
'未完成',
2,
'完成中',
3,
'完成',
NULL) status,
tsum
FROM (SELECT tcode,
tname,
MAX(ttype) ttype,
SUM(tnum) tsum
FROM sony
GROUP BY tcode,
tname);
是的,其实可以不用减1,当sum(ttype)=count(*)即为未完成,当sum(ttype)=3*count(*)即为完成~
其余即完成中
2 case when sum(ttype)/count(*)=1 then '未完成'
3 when sum(ttype)/count(*)=3 then '完成'
4 else '完成中' end from t_test group by tcode,tname;
其实这是投机取巧的办法,如果原表为
tcode tname ttype tnum
1 A 1 100
1 A 1 500
1 B 1 200
1 B 2 600
1 B 3 400
2 A 3 400
2 A 3 700
2 B 1 400
的话就错误了,只要有2,就为完成中,而1,B栏中最大值为3,故结果错误,不过还是谢谢