本文介绍了如何将表达式中的结果列重用于另一个结果列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
SELECT
(SELECT SUM(...) FROM ...) as turnover,
(SELECT SUM(...) FROM ...) as cost,
turnover - cost as profit
确定这是无效的(至少在Postgres中有效),但是如何在查询中实现相同效果而又不重写子查询两次?
Sure this is invalid (at least in Postgres) but how to achieve the same in a query without rewriting the sub-query twice?
推荐答案
像这样:
SELECT
turnover,
cost,
turnover - cost as profit
from (
(SELECT SUM(...) FROM ...) as turnover,
(SELECT SUM(...) FROM ...) as cost
) as partial_sums
这篇关于如何将表达式中的结果列重用于另一个结果列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!