首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

这样的SQL如何写?

2012-02-02 
这样的SQL怎么写???????表TABF1F2f31132521321105224171当f3 2时sum(f2)否则sum(f3)自己写的有错selectca

这样的SQL怎么写???????
表TAB  
F1     F2     f3
1       1       3
2       5       2
1       3       2
1       10     5
2       2       4
1       7       1

当f3> 2   时   sum(f2)
否则     sum(f3)
自己写的有错
select   case   when   f3> 2   then   sum(f2)
                        else   sum(f3)
                        end     num
from   tab
group   by   f1

提示:非group   by   表达式    


[解决办法]
select sum(case when f3> 2 then f2 else f3 end) num from tab group by f1
[解决办法]
正确
[解决办法]
select sum(case when f3> 2 then f2 else 0 end) as sum_f2,
sum(case when f3> 2 then f3 else 0 end) as sum_f3
from tab group by f1

热点排行
Bad Request.