使用sql join从两个表中获取数据总和

时间:2017-02-09 05:06:39

标签: sql sql-server

我有以下两个要结合的查询

SELECT 
    Order_Header.Order_ID, Order_Header.OrderNumber,
    ISNULL(SUM(SplitValue / CostCentre_CCYRate), 0) AS NetPrice_CCY,Issued_Date
FROM 
    Order_Header
LEFT OUTER JOIN 
    Order_CostCentre ON Order_Header.Order_ID = Order_CostCentre.Order_ID
WHERE 
    Order_CostCentre.CostCentre_ID = 3093
    AND Order_Header.Issued_Date BETWEEN 'start date' AND 'end date'
    AND Order_Header.Order_Status IN ('ISSUED', 'PARTIAL', 'RECEIVED', 'CLOSED')
GROUP BY 
    Order_Header.Order_ID


SELECT 
    Expense_Header.Expense_ID, Expense_Header.Exp_Number,
    ISNULL(SUM(SplitValue / CostCentre_CCYRate), 0) AS NetPrice_CCY,
    Exp_Date
FROM
    Expense_Header
LEFT OUTER JOIN 
    Order_CostCentre ON Expense_Header.CostCentre_ID = Order_CostCentre.CostCentre_ID
WHERE 
    Order_CostCentre.CostCentre_ID = 3093
    AND Expense_Header.Exp_Date BETWEEN 'start date' AND 'end date'
    AND Expense_Header.Exp_Status IN ('ACCOUNTS_REVIEW', 'REIMBURSED')
GROUP BY 
    Expense_Header.Expense_ID

我希望将它们结合起来,这样我只能根据Order_CostCentre.CostCentre_ID得到两个表中值的总和。请帮我进行查询。谢谢。

2 个答案:

答案 0 :(得分:0)

一个选项是UNION将您的两个当前查询放在子查询中,然后选择净价的总和,按订单ID分组。请注意,我将两个原始查询中的每一个放入单独的CTE中,以提高可读性,甚至性能。

;WITH cte1 AS (
    SELECT t1.Order_ID,
           t1.OrderNumber, 
           ISNULL(SUM(SplitValue/CostCentre_CCYRate), 0) AS NetPrice_CCY,
           Issued_Date 
    FROM Order_Header t1
    LEFT OUTER JOIN Order_CostCentre t2
        ON t1.Order_ID = t2.Order_ID 
    WHERE t2.CostCentre_ID = 3093 AND
          t1.Issued_Date BETWEEN 'start date' AND 'end date' AND
          t1.Order_Status IN ('ISSUED', 'PARTIAL', 'RECEIVED', 'CLOSED')  
    GROUP BY t1.Order_ID
),

WITH cte2 AS (
    SELECT t1.Expense_ID,
           t1.Exp_Number,     
           ISNULL(SUM(SplitValue/CostCentre_CCYRate), 0) AS NetPrice_CCY,
           Exp_Date
    FROM Expense_Header t1
    LEFT OUTER JOIN Order_CostCentre t2
        ON t1.CostCentre_ID = t2.CostCentre_ID
    WHERE t2.CostCentre_ID = 3093 AND
          t1.Exp_Date BETWEEN 'start date' AND 'end date' AND
          t1.Exp_Status IN ('ACCOUNTS_REVIEW', 'REIMBURSED')
    GROUP BY t1.Expense_ID
)

SELECT t.Order_ID,
       SUM(t.NetPrice_CCY) AS NetPrice_CCY_Total
FROM
(
    SELECT Order_ID, NetPrice_CCY
    FROM cte1
    UNION ALL
    SELECT Expense_ID, NetPrice_CCY
    FROM cte2
) t
GROUP BY t.Order_ID

答案 1 :(得分:0)

这是您的查询。这可能对您有所帮助。

;with merged_data as (

select Order_ID,NetPrice_CCY from(
SELECT Order_Header.Order_ID,Order_Header.OrderNumber,
IsNull(Sum(SplitValue / CostCentre_CCYRate), 0) AS NetPrice_CCY,Issued_Date
FROM Order_Header
LEFT OUTER JOIN Order_CostCentre ON Order_Header.Order_ID = Order_CostCentre.Order_ID
WHERE Order_CostCentre.CostCentre_ID = 3093
AND Order_Header.Issued_Date BETWEEN 'start date' AND 'end date'
AND Order_Header.Order_Status IN ('ISSUED','PARTIAL','RECEIVED','CLOSED')
GROUP BY Order_Header.Order_ID
) as a

union all

select Expense_ID as Order_ID ,NetPrice_CCY from (

SELECT Expense_Header.Expense_ID,Expense_Header.Exp_Number,
IsNull(Sum(SplitValue / CostCentre_CCYRate), 0) AS NetPrice_CCY,Exp_Date
FROM Expense_Header
LEFT OUTER JOIN Order_CostCentre ON Expense_Header.CostCentre_ID = Order_CostCentre.CostCentre_ID
WHERE Order_CostCentre.CostCentre_ID = 3093
AND Expense_Header.Exp_Date BETWEEN 'start date'    AND 'end date'
AND Expense_Header.Exp_Status IN ('ACCOUNTS_REVIEW','REIMBURSED')
GROUP BY Expense_Header.Expense_ID

) as b
) 
select Order_ID ,sum(NetPrice_CCY) from merged_data
group by Order_ID