日期:2014-05-17  浏览次数:20838 次

这样的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