题目大概是这样的:某表:
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);感觉语句有些繁琐,不知道是否有其他简易点的方法?
解决方案 »
- SQL语句 怎样查出A表中 当字段AA 相同,BB字段不同数据
- oracle中自增长的问题
- ibatis框架运用报错,在线坐等高玩指导
- oracle创建触发器 Warning: Trigger created with compilation errors
- oracle如何象sql查询分析器那样去建表 建库 等等
- UNIX下Oracle的性能问题
- sql语句
- 请问怎样在一个已经存在的数据库中创建新的实例?
- 如何在条件判断中对返回符合条件的记录数据进行判断?
- 初学:登录Dba Studio连接数据库时发生"包写入失败"错误?
- 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,故结果错误,不过还是谢谢