我有一个查询里面有一些子查询,我想添加一个求和查询来对它们进行求和。
我怎么能这样做?
示例:
Id,
(SELECT COUNT(*) FROM table1 LEFT JOIN table2 on ...) as col1,
(SELECT COUNT(*) FROM table3 LEFT JOIN table4 on ...) as col2,
** Sum of both col1 and col2 here **
答案 0 :(得分:5)
试试这个:
SELECT ID, col1, col2, [Total] = (col1 + col2)
FROM (
SELECT Id,
(SELECT COUNT(*) FROM table1 LEFT JOIN table2 on ...) as col1,
(SELECT COUNT(*) FROM table3 LEFT JOIN table4 on ...) as col2
FROM [TABLE]) T
希望有所帮助。
答案 1 :(得分:2)
最简单的方法是将所有查询视为子查询
select Id, col1 + col2 as total
from
(<yourCode>) s
因为在select子句中不能在相同的“查询级别”中使用别名。