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

时间:2014-07-12 06:53:22

标签: sql sql-server merge

我尝试使用 UNION ,但只返回 P4 的结果。我现在拥有的是 4个单独的SELECT个查询,它们与GROUP BYP3 P4具有相同的数据>。因此,我想将这些数据合并到 ONE 临时表中。

如何:

  1. 首先将这些表合并到一个表中
  2. 然后是每个工厂的SUM()列
  3. 如下图所示。

    Merge1

    SQL:

    DECLARE @Plants TABLE
    (
        PlantName nvarchar(30),
        MaterialTotalPrice DECIMAL(18,2),
        SumLaborCost DECIMAL(18,2),
        FinalCost DECIMAL(18,2)
    )
    
    DECLARE @PlantName nvarchar(30)
    DECLARE @MaterialTotalPrice DECIMAL(18,2)
    DECLARE @SumLaborCost DECIMAL(18,2)
    DECLARE @FinalCost DECIMAL(18,2)
    
    SELECT
        @PlantName = GetTotalPrice.[PlantName]
        ,@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
            TotalPriceMP3.[PlantName]
            ,SUM(TotalPriceMP3.[MaterialTotalPrice]) AS 'MaterialTotalPrice'
            ,SUM(TotalPriceMP3.[SumLaborCost]) AS 'SumLaborCost'
            ,SUM(TotalPriceMP3.[FinalCost]) AS 'FinalCost'
        FROM Invoice3 AS TotalPriceMP3
        GROUP BY TotalPriceMP3.[PlantName]
        UNION
        SELECT
            TotalPriceMP3.[PlantName]
            ,SUM(TotalPriceMP3.[MaterialTotalPrice]) AS 'MaterialTotalPrice'
            ,SUM(TotalPriceMP3.[SumLaborCost]) AS 'SumLaborCost'
            ,SUM(TotalPriceMP3.[FinalCost]) AS 'FinalCost'
        FROM Invoice4 AS TotalPriceMP3
        GROUP BY TotalPriceMP3.[PlantName]
    
    ) AS GetTotalPrice
    GROUP BY GetTotalPrice.[PlantName]
    INSERT INTO @Plants(PlantName, 
                        MaterialTotalPrice, 
                        SumLaborCost, 
                        FinalCost) 
     VALUES (@PlantName, 
             ISNULL(@MaterialTotalPrice,0), 
             ISNULL(@SumLaborCost,0), 
             ISNULL(@FinalCost,0))
    SELECT * FROM @Plants
    

2 个答案:

答案 0 :(得分:1)

你接近你的工作,你只需再次分组选择

合并: - 在一个屋檐下进行多次进入是一件好事。

为什么使用临时表?如果没有临时表,你会得到理想的东西。

declare @t table ( planname varchar(50), finalcost float)
declare @t1 table ( planname varchar(50), finalcost float)
declare @t2 table ( planname varchar(50), finalcost float)
declare @t3 table ( planname varchar(50), finalcost float)

insert into @t values ( 'P3', 851872.651973)
insert into @t1 values ( 'P3', 1219860.129705)
insert into @t2 values ( 'P4', 7569113.509084)
insert into @t3 values ( 'P4', 24701896.712498)

select planname, SUM(finalcost)  from 
(
select planname, SUM(finalcost) finalcost from @t group by planname
union
select planname, SUM(finalcost) from @t1 group by planname
union
select planname, SUM(finalcost) from @t2 group by planname
union
select planname, SUM(finalcost) from @t3 group by planname
) 
a group by planname 

答案 1 :(得分:1)

知道什么时候不要单独使用union非常重要。

declare @t table ( planname varchar(50), finalcost float)
declare @t1 table ( planname varchar(50), finalcost float)
declare @t2 table ( planname varchar(50), finalcost float)
declare @t3 table ( planname varchar(50), finalcost float)

insert into @t values ( 'P3', 100.00)
insert into @t1 values ( 'P3', 100.00)
insert into @t2 values ( 'P3', 100.00)
insert into @t3 values ( 'P3', 100.00)

select planname, SUM(finalcost)  from 
(
select planname, SUM(finalcost) finalcost from @t group by planname
union ALL
select planname, SUM(finalcost) from @t1 group by planname
union ALL
select planname, SUM(finalcost) from @t2 group by planname
union ALL
select planname, SUM(finalcost) from @t3 group by planname
) 
a group by planname
;

结果= 400

如果您单独使用union,则结果为100.00