如何通过另一个计算列使用计算列 [英] How to use a calculated column by another calculated column
本文介绍了如何通过另一个计算列使用计算列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Sqlite 中有一个涉及复杂列计算的查询,比方说:
SELECT 1+1 AS a;
我想将此计算选择为 a
,但我还需要将其用作另一个计算的组件:
SELECT 1+1 AS a, a+2 AS b;
不幸的是,这会产生错误:
错误:没有这样的列:a
我知道我可以简单地为 b
再次重复计算:
SELECT 1+1 AS a, 1+1+2 AS b;
但是假设 1+1
是一些复杂且昂贵的操作,有什么方法可以让我稍后在 SELECT
中引用它而不必重新计算它?>
解决方案
您需要使用子查询.
SELECT c.d AS a, c.d + 2 AS b发件人(SELECT 1+1 AS d) c
结果
<前>||乙 |---------|2 |4 |I have a query in Sqlite that involves a complicated column calculation, let's say:
SELECT 1+1 AS a;
I want to have this calculation selected as a
, but I also need to use it as a component of another calculation:
SELECT 1+1 AS a, a+2 AS b;
Unfortunately this produces the error:
Error: no such column: a
I know that I could simply repeat the calculation again for b
:
SELECT 1+1 AS a, 1+1+2 AS b;
But assuming 1+1
is some complicated and expensive operation, is there any way that I can reference it later in the SELECT
without having to recalculate it?
解决方案
You need to use a sub-query.
SELECT c.d AS a, c.d + 2 AS b
FROM
(SELECT 1+1 AS d) c
Result
| a | b | --------- | 2 | 4 |
这篇关于如何通过另一个计算列使用计算列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文