如何将2个不同SQL查询的值相加?

时间:2017-02-15 18:43:05

标签: sql sql-server

我对使用SQL有点新意。我有两个不同的查询,我需要在每个查询中添加或总和其中一列的值。结果的示例代码和屏幕截图如下:

WITH tbl AS 
(   
    SELECT 
        y.Location, 
        IIF(Status = 'Completed', 1,0) as Completed,  
        IIF(Status = 'Pending', 1, 0) as Pending,
        IIF(Status = 'Scheduled', 1, 0) as Scheduled,
        IIF(Satisfied = 'Satisfied', 1 , 0 ) As Satisfied, 
        IIF(Attrition IN ('Red', 'Amber'),1,0) As Attrition
    FROM
        order x
    LEFT JOIN 
        roster y ON x.customerID = y.cID 
    LEFT JOIN 
        tbl_calendar z ON (x.starttime between z.datestart AND z.dateend)
    WHERE 
        y.LOCATION IS NOT NULL 
        AND y.Location <> 'Ireland' 
        AND z.month ='2' 
        AND z.week IN (SELECT * FROM dbo.split('1,2,3',','))
        AND z.year = '2017'
)
SELECT 
    Location, 
    SUM(Completed) AS Completed, 
    SUM(Pending) AS Pending, 
    SUM(Scheduled) AS Scheduled, 
    SUM(Satisfied) as Satisfied, 
    SUM(Attrition) as Attrition  
FROM
    tbl 
GROUP BY 
    Location

enter image description here

这是第二个查询:

SELECT 
    y.location, count (*) as qc 
FROM 
    customer_quality x
LEFT JOIN 
    roster y ON x.customerEID = y.cEID
LEFT JOIN 
    tbl_calendar z ON (x.DateTimeDelivered BETWEEN z.datestart AND z.dateend)
WHERE 
    y.location IS NOT NULL
    AND x.status = '4' 
    AND z.month = '2' 
    AND z.week IN (SELECT * FROM dbo.split('1,2,3',',')) 
    AND z.year = '2017'
GROUP BY
    y.Location

enter image description here

我需要做的是将查询1中已完成订单总数的值添加到查询2中的总质量客户。

例如:

如果在里斯本有208名完成客户,则其他4名优质客户将会增加,总数应为212名。

它会显示如下:

Location    Completed   Pending     Scheduled   Satisfied   Attrition
Kuala Lumpur    388      76           9            388         3
Lisbon          212      92          29            207         1
Manila         3535      97         167            662        24
Mumbai          538      50          54            2100        6
Warsaw          147      38           4            145         9

3 个答案:

答案 0 :(得分:0)

按照https://msdn.microsoft.com/en-us/library/ms175972.aspx#C-Using-multiple-CTE-definitions-in-a-single-query中的描述创建2个CTE,然后将它们连接在一起,并在 SELECT 最终子句中执行所需的计算

答案 1 :(得分:0)

我没有要测试的数据,但这应该让你开始。

With tbl  as 
(   SELECT y.Location, 
             IIF(Status = 'Completed', 1,0) as Completed,  
             IIF(Status = 'Pending', 1, 0) as Pending,
             IIF(Status = 'Scheduled', 1, 0) as Scheduled,
             IIF(Satisfied = 'Satisfied', 1 , 0 ) As Satisfied, 
             IIF(Attrition IN ('Red', 'Amber'),1,0) As Attrition
    FROM order x
         LEFT JOIN roster y ON x.customerID=y.cID 
         LEFT JOIN tbl_calendar z ON (x.starttime between z.datestart AND z.dateend)
    WHERE y.LOCATION IS NOT NULL 
             AND y.Location <> 'Ireland' 
             AND z.month ='2' 
             AND z.week IN (select * from dbo.split('1,2,3',','))
             AND z.year='2017'
)

SELECT Location, 
       SUM(Completed) AS Completed , 
       SUM (Pending) AS Pending, 
       SUM(Scheduled) AS Scheduled, 
       SUM(Satisfied) as Satisfied, 
       SUM(Attrition) as Attrition 
INTO #TABLE1 
FROM tbl 
GROUP BY Location

SELECT y.location, count (*) as qc 
INTO #TABLE2
FROM customer_quality x
     LEFT JOIN roster y ON x.customerEID = y.cEID
     LEFT JOIN tbl_calendar z ON (x.DateTimeDelivered between z.datestart 
AND z.dateend)
WHERE y.location is not null 
      AND x.status='4' AND z.month = '2' 
      AND z.week IN (select * FROM dbo.split('1,2,3',',')) AND z.year='2017'
GROUP by y.Location



SELECT t1.Location
        , (t1.Completed + t2.qc) AS Completed
        , t1.Pending
        , t1.Scheduled
        , t1.Satisfied
        , t1.Attrition
FROM #TABLE1 t1
    JOIN #TABLE2 t2 ON
        t1.Location = t2.Location



DROP TABLE #TABLE1
DROP TABLE #TABLE2

答案 2 :(得分:0)

或者您可以使用

将第一个查询的结果存储在临时表中
SELECT * INTO #temp1 FROM (
    SELECT Location, 
       SUM(Completed) AS Completed , 
       SUM (Pending) AS Pending, 
       SUM(Scheduled) AS Scheduled, 
       SUM(Satisfied) as Satisfied, 
       SUM(Attrition) as Attrition  
    FROM tbl 
    GROUP BY Location
)

SELECT * INTO #temp2 FROM (
    SELECT y.location, count (*) as qc 
    FROM customer_quality x
         LEFT JOIN roster y ON x.customerEID = y.cEID
         LEFT JOIN tbl_calendar z ON (x.DateTimeDelivered between z.datestart AND z.dateend)
    WHERE y.location is not null 
          AND x.status='4' AND z.month = '2' 
          AND z.week IN (select * FROM dbo.split('1,2,3',',')) AND z.year='2017'
    GROUP by y.Location
)

最后总结查询可以是:

SELECT t1.location
, t1.COMPLETED + t2.cq AS Completed
, t1.Pending
, t1.Scheduled
, t1.Satisfied
, t1.Attrition
FROM #temp1 t1
JOIN #temp2 t2 on t1.location = t2.location

或采取将CTE串联起来的方法。