如何使用SUM()并使用SQL将表合并到一个表中

时间:2014-07-12 02:27:25

标签: sql sql-server merge

我所拥有的是 4个单独的SELECT个查询,并且它们具有相同的数据。因此,我想将这些数据合并到 ONE 临时表中。

如何:

  1. 首先将4个表合并到一个表中
  2. 然后是SUM()列
  3. 如下图所示。我已经尝试但它没有用。

    Merge

    SQL:

    DECLARE @Plants TABLE
    (
        PlantName nvarchar(30),
        MaterialTotalPrice DECIMAL,
        SumLaborCost DECIMAL,
        FinalCost DECIMAL
    )
    
    DECLARE @MaterialTotalPrice DECIMAL
    DECLARE @SumLaborCost DECIMAL
    DECLARE @FinalCost DECIMAL
    
    SELECT
        @MaterialTotalPrice = SUM(GetTotalPrice.[MaterialTotalPrice])
        ,@SumLaborCost = SUM(GetTotalPrice.[SumLaborCost])
        ,@FinalCost = SUM(GetTotalPrice.[FinalCost])
    FROM
    (
    
        SELECT
            TotalPriceMP4.[PlantName]
            ,SUM(TotalPriceMP4.[MaterialTotalPrice]) AS 'MaterialTotalPrice'
            ,SUM(TotalPriceMP4.[SumLaborCost]) AS 'SumLaborCost'
            ,SUM(TotalPriceMP4.[FinalCost]) AS 'FinalCost'
        FROM Invoice1 AS TotalPriceMP4
        GROUP BY TotalPriceMP4.[PlantName]
    
        SELECT
            TotalPriceMP4.[PlantName]
            ,SUM(TotalPriceMP4.[MaterialTotalPrice]) AS 'MaterialTotalPrice'
            ,SUM(TotalPriceMP4.[SumLaborCost]) AS 'SumLaborCost'
            ,SUM(TotalPriceMP4.[FinalCost]) AS 'FinalCost'
        FROM Invoice2 AS TotalPriceMP4
        GROUP BY TotalPriceMP4.[PlantName]
    
        SELECT
            TotalPriceMP4.[PlantName]
            ,SUM(TotalPriceMP4.[MaterialTotalPrice]) AS 'MaterialTotalPrice'
            ,SUM(TotalPriceMP4.[SumLaborCost]) AS 'SumLaborCost'
            ,SUM(TotalPriceMP4.[FinalCost]) AS 'FinalCost'
        FROM Invoice3 AS TotalPriceMP4
        GROUP BY TotalPriceMP4.[PlantName]
    
        SELECT
            TotalPriceMP4.[PlantName]
            ,SUM(TotalPriceMP4.[MaterialTotalPrice]) AS 'MaterialTotalPrice'
            ,SUM(TotalPriceMP4.[SumLaborCost]) AS 'SumLaborCost'
            ,SUM(TotalPriceMP4.[FinalCost]) AS 'FinalCost'
        FROM Invoice4 AS TotalPriceMP4
        GROUP BY TotalPriceMP4.[PlantName]
    
    ) AS GetTotalPrice
    INSERT INTO @Plants(PlantName, MaterialTotalPrice, SumLaborCost, FinalCost) VALUES ('P4', ISNULL(@MaterialTotalPrice,0), ISNULL(@SumLaborCost,0), ISNULL(@FinalCost,0))
    SELECT * FROM @Plants
    

1 个答案:

答案 0 :(得分:0)

您只需将UNION结果合并在一起,将这些(相同的)结果集合并为一个。

DECLARE @Plants TABLE
(
    PlantName nvarchar(30),
    MaterialTotalPrice DECIMAL,
    SumLaborCost DECIMAL,
    FinalCost DECIMAL
)

DECLARE @MaterialTotalPrice DECIMAL
DECLARE @SumLaborCost DECIMAL
DECLARE @FinalCost DECIMAL

SELECT
    @MaterialTotalPrice = SUM(GetTotalPrice.[MaterialTotalPrice])
    ,@SumLaborCost = SUM(GetTotalPrice.[SumLaborCost])
    ,@FinalCost = SUM(GetTotalPrice.[FinalCost])
FROM
(
    SELECT
        TotalPriceMP4.[PlantName]
        ,SUM(TotalPriceMP4.[MaterialTotalPrice]) AS 'MaterialTotalPrice'
        ,SUM(TotalPriceMP4.[SumLaborCost]) AS 'SumLaborCost'
        ,SUM(TotalPriceMP4.[FinalCost]) AS 'FinalCost'
    FROM Invoice1 AS TotalPriceMP4
    GROUP BY TotalPriceMP4.[PlantName]
    UNION
    SELECT
        TotalPriceMP4.[PlantName]
        ,SUM(TotalPriceMP4.[MaterialTotalPrice]) AS 'MaterialTotalPrice'
        ,SUM(TotalPriceMP4.[SumLaborCost]) AS 'SumLaborCost'
        ,SUM(TotalPriceMP4.[FinalCost]) AS 'FinalCost'
    FROM Invoice2 AS TotalPriceMP4
    GROUP BY TotalPriceMP4.[PlantName]
    UNION
    SELECT
        TotalPriceMP4.[PlantName]
        ,SUM(TotalPriceMP4.[MaterialTotalPrice]) AS 'MaterialTotalPrice'
        ,SUM(TotalPriceMP4.[SumLaborCost]) AS 'SumLaborCost'
        ,SUM(TotalPriceMP4.[FinalCost]) AS 'FinalCost'
    FROM Invoice3 AS TotalPriceMP4
    GROUP BY TotalPriceMP4.[PlantName]
    UNION
    SELECT
        TotalPriceMP4.[PlantName]
        ,SUM(TotalPriceMP4.[MaterialTotalPrice]) AS 'MaterialTotalPrice'
        ,SUM(TotalPriceMP4.[SumLaborCost]) AS 'SumLaborCost'
        ,SUM(TotalPriceMP4.[FinalCost]) AS 'FinalCost'
    FROM Invoice4 AS TotalPriceMP4
    GROUP BY TotalPriceMP4.[PlantName]

) AS GetTotalPrice
INSERT INTO @Plants(PlantName, MaterialTotalPrice, SumLaborCost, FinalCost) VALUES ('P4', ISNULL(@MaterialTotalPrice,0), ISNULL(@SumLaborCost,0), ISNULL(@FinalCost,0))
SELECT * FROM @Plants