sql语句,对刚查询的两个列值 进行计算
selectSUM(FEE),SUM(RC),SUM(FEE)/SUM(RC),SUM(casewhenname=athenFEEelse0end)asa,SUM(cas...
select SUM(FEE) ,SUM(RC),SUM(FEE)/SUM(RC),
SUM(case when name=a then FEE else 0 end) as a,
SUM(case when name=a then RC else 0 end) as b,
case when HOS_CANTON=CANTON_CODE_XZ then a/b else 0 end,//?
最上面是直接取出sum和,然后进行了SUM(INHOS_FEE)/SUM(INHOS_RC)做运算,但是下面有条件了,应该怎么做别名为a和b的除法运算呢?我要的是一个sql语句而且后面还有很多类似的运算 展开
SUM(case when name=a then FEE else 0 end) as a,
SUM(case when name=a then RC else 0 end) as b,
case when HOS_CANTON=CANTON_CODE_XZ then a/b else 0 end,//?
最上面是直接取出sum和,然后进行了SUM(INHOS_FEE)/SUM(INHOS_RC)做运算,但是下面有条件了,应该怎么做别名为a和b的除法运算呢?我要的是一个sql语句而且后面还有很多类似的运算 展开
2个回答
展开全部
做成一个CTE或者子查询。
select a, b,case when HOS_CANTON=CANTON_CODE_XZ then a/b else 0 end as c
from
(
select
SUM(case when name=a then FEE else 0 end) as a,
SUM(case when name=a then RC else 0 end) as b,
from tablename
) as t
select a, b,case when HOS_CANTON=CANTON_CODE_XZ then a/b else 0 end as c
from
(
select
SUM(case when name=a then FEE else 0 end) as a,
SUM(case when name=a then RC else 0 end) as b,
from tablename
) as t
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询