我希望在一个语句中添加两个值,而我目前对如何解决此问题感到茫然,这应该是我的陈述应该很简单:
SELECT COUNT(UserModified) as SecondTally
FROM JobAgents
WHERE UserModified = 1
然后从下面的这个值中减去上面的值。
SELECT SUM(EmailsSent) as FirstTally
FROM JobAgentEmails
WHERE Date BETWEEN '7/1/2012 12:00:00 AM' AND '7/31/2012 11:59:59 PM'
对此的任何帮助将不胜感激。非常感谢。
答案 0 :(得分:3)
最简单的方法是简单地将结果与两个子查询相结合,这两个子查询可以存储在变量中,也可以简单地组合在一个查询中,如下所示:
SELECT
(SELECT SUM(EmailsSent) as FirstTally
FROM JobAgentEmails
WHERE Date BETWEEN '7/1/2012 12:00:00 AM' AND '7/31/2012 11:59:59 PM')
- (SELECT COUNT(UserModified) as SecondTally
FROM JobAgents
WHERE UserModified = 1)
答案 1 :(得分:2)
您可以使用交叉连接来实现此目的,将值集中在一起:
select jae.FirstTallyl - ja.SecondTally
from (SELECT COUNT(UserModified) as SecondTally
FROM JobAgents
WHERE UserModified = 1
) ja cross join
(SELECT SUM(EmailsSent) as FirstTally
FROM JobAgentEmails
WHERE Date BETWEEN '7/1/2012 12:00:00 AM' AND '7/31/2012 11:59:59 PM'
) jae
答案 2 :(得分:1)
这是一种方式:
DECLARE @FirstTally int;
DECLARE @SecondTally int;
SELECT @SecondTally = COUNT(UserModified)
FROM JobAgents
WHERE UserModified = 1
SELECT @FirstTally = SUM(EmailsSent)
FROM JobAgentEmails
WHERE Date BETWEEN '7/1/2012 12:00:00 AM' AND '7/31/2012 11:59:59 PM'
SELECT @FirstTalley - @SecondTalley AS Result
答案 3 :(得分:1)
select
(SELECT SUM(EmailsSent) as FirstTally
FROM JobAgentEmails
WHERE Date BETWEEN '7/1/2012 12:00:00 AM' AND '7/31/2012 11:59:59 PM')
-
(SELECT COUNT(UserModified) as SecondTally
FROM JobAgents
WHERE UserModified = 1)
AS 'result';