如果我有以下选择查询:
select col1, col2, col3, sum(value1) as col4, col5
from table1
group by col1, col2, col3, col5
如何添加col6 = col4 / col5?
答案 0 :(得分:1)
您无法访问SELECT
子句中的别名。所以你必须重复sum(value1)
:
select col1, col2, col3,
sum(value1) as col4,
col5,
sum(value1) / col5 as col6
from table1
group by col1, col2, col3, col5
答案 1 :(得分:1)
在派生表中执行GROUP BY
:
select col1, col2, col3, col4, col5, col4/col5 as col6
from
(
select col1, col2, col3, sum(value1) as col4, col5
from table1
group by col1, col2, col3, col5
) dt
答案 2 :(得分:0)
您可以在select语句中执行操作。但是,您不能在其中使用SQL语句的别名。所以你需要再次计算col4。只需添加sum(value1)/col5 as col6
。
select col1, col2, col3, sum(value1) as col4, col5, sum(value1)/col5 as col6
from table1
group by col1, col2, col3, col5