SQL Server - 减去两个SQL查询的结果

时间:2017-09-25 20:38:27

标签: sql sql-server tsql subquery

我尝试在SQL Server中使用Subquery进行两次查询。

我想要实现的目标是计算属于" ProductX Promotion"的客户数量。并减去属于" ProductX Promotion"的客户数量。并且在过去60天内实际购买了ProductX。这为我提供了未购买ProductX的注册客户数量,以获得更好的营销洞察力。

最初我只有COUNT()因此很容易减去这两个。现在要求是按客户的State和Zip级别细分计数。问题出现在这里,因为我不能使用直接减法,我尝试不存在但没有工作,我尝试了JOIN但仍然没有运气。我知道解决方案非常简单,但我想不出任何问题。 这是原始代码,其中包含对特定列的含义的评论,

SELECT 
(SELECT COUNT (DISTINCT (c.CustomerNumber))
FROM Customer c
INNER JOIN 
TransactionDetail t
ON 
c.CustomerNumber = t.CustomerNumber
WHERE c.ProductXPref = 1 --Indicates if Customer was part of ProductX promotion program.
AND t.TransactionDate > DATEADD(d, -60, getdate()))
-
(SELECT COUNT (DISTINCT c.CustomerNumber)
FROM Customer c
INNER JOIN
TransactionDetail t
ON 
c.CustomerNumber = t.CustomerNumber
WHERE t.ProductXIndicator = 1 --Indicates if ProductX was purchased
AND t.TransactionDate > DATEADD(DD, -60, getdate()) 
AND c.ProductXPref = 1 --Indicates if Customer was part of ProductX promotion program.
) AS 'Column Name' INTO #TempTable1

这是我尝试实施的内容,

SELECT 
(SELECT c.Zip, c.State, COUNT (DISTINCT (c.CustomerNumber))
FROM Customer c
INNER JOIN 
TransactionDetail t
ON 
c.CustomerNumber = t.CustomerNumber
WHERE c.ProductXPref = 1
AND t.TransactionDate > DATEADD(d, -60, getdate())
GROUP BY c.Zip, c.State)
-
(SELECT c.Zip, c.State, COUNT (DISTINCT c.CustomerNumber)
FROM Customer c
INNER JOIN
TransactionDetail t
ON 
c.CustomerNumber = t.CustomerNumber
WHERE t.ProductXIndicator = 1
AND t.TransactionDate > DATEADD(DD, -60, getdate()) 
AND c.ProductXPref = 1
GROUP BY c.Zip, c.State) AS 'Column Name' INTO #TempTable1

这是我看到的错误,

  

Msg 116,Level 16,State 1,Line 129   当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。

任何指针都会有所帮助。感谢。

3 个答案:

答案 0 :(得分:3)

我认为你使它变得比它需要的复杂得多 - 第一个查询可以写成

SELECT COUNT(DISTINCT c.CustomerNumber)
FROM Customer c
INNER JOIN TransactionDetail t ON c.CustomerNumber = t.CustomerNumber
WHERE 
    c.ProductXPref = 1 --Indicates if Customer was part of ProductX promotion program.
AND t.TransactionDate > DATEADD(d, -60, getdate()))
AND  t.ProductXIndicator <> 1

正如您所看到的,如果您不在原始计数中包含这些行,则与执行其他查询和减去相同。

所以第二个就是

SELECT c.Zip, c.State, COUNT(DISTINCT c.CustomerNumber)
FROM Customer c
INNER JOIN TransactionDetail t ON c.CustomerNumber = t.CustomerNumber
WHERE 
    c.ProductXPref = 1 --Indicates if Customer was part of ProductX promotion program.
AND t.TransactionDate > DATEADD(d, -60, getdate()))
AND  t.ProductXIndicator <> 1
GROUP BY c.Zip, c.State

答案 1 :(得分:0)

这应该有效:

SELECT 
    c.Zip, c.State,COUNT (DISTINCT c.CustomerNumber)
FROM 
    Customer c
        INNER JOIN TransactionDetail t
            ON 
                c.CustomerNumber = t.CustomerNumber AND
                c.ProductXPref = 1 --Indicates if Customer was part of ProductX promotion program.
                AND t.TransactionDate > DATEADD(DD, -60, getdate()) 
WHERE t.ProductXIndicator <> 1 

GROUP BY c.Zip, c.State

答案 2 :(得分:0)

经过一些验证后,这些数字与我预期的数字不符,因此我尝试了不同的逻辑,这些逻辑工作正常,并返回了我想要的内容。所以在这里发布答案,再次感谢所有的帮助。

SELECT d.State, d.Zip, COUNT(DISTINCT(d.CustomerNumber)) AS 'Active Customers'
    INTO #ProductDifference
    FROM Customer c
    INNER JOIN 
    TransactionDetail t
    ON 
    c.CustomerNumber = t.CustomerNumber
    WHERE d.ProductXPref = 1
    AND t.TransactionDate > DATEADD(d, -60, getdate())
AND c.CustomerNumber
NOT IN
(SELECT DISTINCT d.[CustomerNumber] 
FROM Customer d 
     INNER JOIN
    TransactionDetail f
     ON 
     d.CustomerNumber = f.CustomerNumber
WHERE f.ProductXIndicator = 1
    AND f.TransactionDate > DATEADD(DD, -60, getdate()) 
    AND d.ProductXPref = 1)
GROUP BY c.State, c.Zip