我所拥有的是 4个单独的SELECT
个查询,并且它们具有相同的数据。因此,我想将这些数据合并到 ONE 临时表中。
如何:
如下图所示。我已经尝试但它没有用。
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
答案 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