添加两个选择计数查询的总和

mHe*_*pMe 0 sql sql-server

我正在使用SQL Server 2012.

我有两个表,我正在尝试查看两个表包含多少行.他们没有可以加入的共同领域.以下是我目前的查询,显然不起作用.应该怎么样?

;with tblOne as
(
    select count(*) numRows from tblOne where DateEx = '2015-10-27'
),
tblTwo as
(
    select count(*) numRows from tblTwo where Status = 0
)
select tblOne.numRows + tblTwo.numRows
Run Code Online (Sandbox Code Playgroud)

D S*_*ley 6

你不需要CTE; 你可以把它们改成子查询:

select 
    (select count(*) numRows from tblOne where DateEx = '2015-10-27') +   
    (select count(*) numRows from tblTwo where Status = 0)
Run Code Online (Sandbox Code Playgroud)

如果您真的想要使用CTE,那么只需执行隐式CROSS JOIN:

with tblOne as
(
    select count(*) numRows from tblOne where DateEx = '2015-10-27'
),
tblTwo as
(
    select count(*) numRows from tblTwo where Status = 0
)
select tblOne.numRows + tblTwo.numRows
FROM tblOne, tblTwo
Run Code Online (Sandbox Code Playgroud)