我正在尝试完成以下操作,我不确定是否可行。我有一个SELECT
语句,其中包含两个表列的内部SELECT
,如下所示:
SELECT
col1,
col2,
(SELECT SUM(col1)
FROM table2)
AS FirstResultToAdd,
(SELECT SUM(col2)
FROM table3)
AS SecondResultToAdd,
FROM Table1
所以我的问题是:是否可以执行计算,例如执行SUM
" FirstResultToAdd"和" SecondResultToAdd,并将其作为单个列结果返回到" Table1"?另外要记住,我已经排除了表的任何连接以保持示例简单。
答案 0 :(得分:1)
我相信你想对Sub-query
添加两个sub-query
结果
SELECT col1,
col2,
(SELECT col1
FROM table2)
AS FirstResultToAdd,
(SELECT col2
FROM table3)
AS SecondResultToAdd,
(SELECT col1
FROM table2)
+
(SELECT col2
FROM table3)
AS total
FROM table1
为了使查询更具可读性,您可以将原始查询设为Sub-Select
并执行Outer query
答案 1 :(得分:0)
再嵌套一次......
select col1, col2, sum( FirstResultToAdd )
from (
SELECT
col1,
col2,
(SELECT col1
FROM table2)
AS FirstResultToAdd,
(SELECT col2
FROM table3)
AS SecondResultToAdd,
FROM Table1
)
答案 2 :(得分:0)
修改:固定分组
试试这个:
Select A.Col1,
A.Col2,
(B.Col3 + C.Col4)
From(
(Select Col1,
Col2
From [Table1]) A
Inner join (Select Sum(Col3) AS Col3
From [Table2]) B on 1 = 1
Inner join (Select Sum(Col4) AS Col4
From [Table3]) C on 1 = 1
)
Group By A.Col1,
A.Col2,
B.Col3,
C.Col4