我有两个问题:
SELECT SUM(DATALENGTH(Money)) / 1048576.0 from Money
SELECT SUM(DATALENGTH(Cc)) / 1048576.0 from CC
如果单独跑,他们会返回20和30。 我想结合这些来得到50的结果。
这可以在一行中完成吗?而是分配查询1和2的两个变量值并添加它们?
答案 0 :(得分:1)
执行此操作的标准方法是使用cross join
:
select val1 + val2
from (SELECT SUM(DATALENGTH(Money)) / 1048576.0 as val1 from Money
) t1 cross join
(SELECT SUM(DATALENGTH(Cc)) / 1048576.0 as val2 from CC
) t2
答案 1 :(得分:1)
select sum(sm) as total_sum
from (
SELECT SUM(DATALENGTH(Money)) / 1048576.0 as sm
from Money
union all
SELECT SUM(DATALENGTH(Cc)) / 1048576.0
from CC
) t
答案 2 :(得分:1)
这应该可以解决问题:
SELECT
(SUM(DATALENGTH(Money)) / 1048576.0) + SUM(DATALENGTH(Cc)) / 1048576.0
from Money, CC
答案 3 :(得分:0)
我相信这会奏效:
Select
(SELECT SUM(DATALENGTH(Money)) / 1048576.0 from Money)
+ (SELECT SUM(DATALENGTH(Cc)) / 1048576.0 from CC)
至少它在我的数据库引擎中。
答案 4 :(得分:0)
SELECT SUM(S)
FROM (
SELECT SUM(DATALENGTH(Money)) / 1048576.0 S from Money
UNION ALL
SELECT SUM(DATALENGTH(Cc)) / 1048576.0 from CC
) Q;