2017-02-15 49 views
2

顯示我有三個表:表1(AF),表2(MC)和表3(SC)SUM和JOIN到在一個記錄

我現在有一個查詢這些表爲這樣:

SELECT 
AF.[Product Number], 
AF.[Week End Date], 
AF.[Inv Change Cost], 
AF.[Gross Sales Lbs], 
AF.[Production Lbs], 
MC.[Actual Usage Cost] AS MeatCost, 
SC.[Actual Usage Cost] AS SeasonCost 
FROM Table1 AF 
FULL JOIN Table2 MC ON MC.[Product Number] = AF.[Product Number] AND MC.[Week Ending Date] = AF.[Week End Date] 
FULL JOIN Table3 SC ON SC.[Product Number] = AF.[Product Number] AND SC.[Week Ending Date] = MC.[Week Ending Date] 
WHERE 
AF.[Product Number] = '96443' AND 
AF.[Week End Date] = '2/4/2017' AND 
MC.[Rout Line Name] = 'Total Cost' AND 
SC.[Rout Line Name] = 'Total Cost' 

返回以下內容:

Prod # | WE Date | IC Cost | Gross Sales | Prod Lbs | Meat Cost | Season Cost | 
96443 2/4/2017 -123456 1000   2000  5000  10000 
96443 2/4/2017 -123456 1000   2000  5000  20000 
96443 2/4/2017 -123456 1000   2000  6000  10000 
96443 2/4/2017 -123456 1000   2000  6000  20000 

不過,我希望能夠總結肉類和賽季表是這樣的:

Prod # | WE Date | IC Cost | Gross Sales | Prod Lbs | Meat Cost | Season Cost | 
96443 2/4/2017 -123456 1000   2000  11000  30000 

我試過下面的語句,但我得到的所有四個記錄總結爲兩個和這不是我想要的返回值:它返回的結果

SELECT 
AF.[Product Number], 
AF.[Week End Date], 
AF.[Inv Change Cost], 
AF.[Gross Sales Lbs], 
AF.[Production Lbs], 
SUM(MC.[Actual Usage Cost]) AS MeatCost, 
SUM(SC.[Actual Usage Cost]) AS SeasonCost 
FROM Table1 AF 
JOIN Table2 MC ON MC.[Product Number] = AF.[Product Number] AND MC.[Week Ending Date] = AF.[Week End Date] 
JOIN Table3 SC ON SC.[Product Number] = AF.[Product Number] AND SC.[Week Ending Date] = MC.[Week Ending Date] 
WHERE 
AF.[Product Number] = '96443' AND 
AF.[Week End Date] = '2/4/2017' AND 
MC.[Rout Line Name] = 'Total Cost' AND 
SC.[Rout Line Name] = 'Total Cost' 
GROUP BY 
AF.[Product Number], 
AF.[Week End Date], 
AF.[Inv Change Cost], 
AF.[Gross Sales Lbs], 
AF.[Production Lbs]; 

Prod # | WE Date | IC Cost | Gross Sales | Prod Lbs | Meat Cost | Season Cost | 
96443 2/4/2017 -123456 1000   2000  22000  60000 

我是否需要總結所有的字段以獲得期望的結果?

UPDATE:

我已經嘗試這樣的說法:

SELECT 
AF.[Product Number], 
AF.[Week End Date], 
AF.[Inv Change Cost], 
AF.[Gross Sales Lbs], 
AF.[Production Lbs], 
MC.MeatCost, 
SC.SeasonCost 
FROM Table1 AF 
    JOIN(
     SELECT [Product Number], [Week Ending Date], SUM([Actual Usage Cost]) AS MeatCost 
     FROM Table2 
     GROUP BY [Product Number], [Week Ending Date] 
     )MC ON 
     AF.[Product Number] = MC.[Product Number] AND 
     AF.[Week End Date] = MC.[Week Ending Date] 
    JOIN(
     SELECT [Product Number], [Week Ending Date], SUM([Actual Usage Cost]) AS SeasonCost 
     FROM Table3 
     GROUP BY [Product Number], [Week Ending Date] 
     )SC ON 
     SC.[Product Number] = MC.[Product Number] AND 
     SC.[Week Ending Date] = MC.[Week Ending Date] 
WHERE 
AF.[Product Number] = '96443' AND 
AF.[Week End Date] = '2/4/2017'; 

但是,我得到了相同的結果,以前的嘗試。

+0

您是否有字段?什麼是季節成本? – manderson

+0

我有更多的領域,但其中大多數都是獨特的各自的表 – jDave1984

+0

您的查詢按預期工作,但需要知道96443產品號之間的區別...表3中的某些東西。 – manderson

回答

2

使用這個例子,嘗試這樣的事情......

CREATE TABLE #Table1 (id INT IDENTITY(1,1), ProdNum INT, WeekEndDate DATETIME, InvChangeCost NUMERIC(18,2), GrossSales NUMERIC(18,2), ProductionLibs NUMERIC(18,2)) 

INSERT INTO #Table1(ProdNum ,WeekEndDate ,InvChangeCost ,GrossSales ,ProductionLibs) 
VALUES(96443, '2/4/2017', -123456, 1000,   2000) 

CREATE TABLE #Table2 (id INT IDENTITY(1,1), ProdNum INT, WeekEndDate DATETIME, MeatCost NUMERIC(18,2)) 

INSERT INTO #Table2(ProdNum ,WeekEndDate ,MeatCost) 
VALUES(96443, '2/4/2017', 5000) 
INSERT INTO #Table2(ProdNum ,WeekEndDate ,MeatCost) 
VALUES(96443, '2/4/2017', 6000) 

CREATE TABLE #Table3 (id INT IDENTITY(1,1), ProdNum INT, WeekEndDate DATETIME, SeasonCost NUMERIC(18,2)) 

INSERT INTO #Table3(ProdNum ,WeekEndDate ,SeasonCost) 
VALUES(96443, '2/4/2017', 1000) 
INSERT INTO #Table3(ProdNum ,WeekEndDate ,SeasonCost) 
VALUES(96443, '2/4/2017', 2000) 

SELECT t1.* 
     , t2.MeatCost 
     , t3.SeasonCost 
FROM #Table1 t1 
    JOIN (
      SELECT ProdNum, WeekEndDate, SUM(MeatCost) AS MeatCost 
      FROM #Table2 
      GROUP BY ProdNum, WeekEndDate 
     )t2 ON 
     t1.ProdNum = t2.ProdNum AND 
     t1.WeekEndDate = t2.WeekEndDate 
    JOIN (
      SELECT ProdNum, WeekEndDate, SUM(SeasonCost) AS SeasonCost 
      FROM #Table3 
      GROUP BY ProdNum, WeekEndDate 
     )t3 ON 
     t3.ProdNum = t2.ProdNum AND 
     t3.WeekEndDate = t2.WeekEndDate 


DROP TABLE #Table1 
DROP TABLE #Table2 
DROP TABLE #Table3 
+0

這與我將提議使用CTE代替JOIN中的子查詢的解決方案非常相似。 –

+0

@ digital.aaron向我們展示。我不使用許多CTE並希望看到。 – manderson

+0

它使用相同的想法。查找肉類成本和季節成本的小計,然後將其加入最終的SELECT中。我喜歡CTEs,因爲它們易於閱讀。 –

0

你可以試試這個...

SELECT 
AF.[Product Number], 
MAX(AF.[Week End Date]), 
MAX(AF.[Inv Change Cost]), 
MAX(AF.[Gross Sales Lbs]), 
MAX(AF.[Production Lbs]), 
SUM(MC.[Actual Usage Cost]) AS MeatCost, 
SUM(SC.[Actual Usage Cost]) AS SeasonCost 
FROM Table1 AF 
JOIN Table2 MC ON MC.[Product Number] = AF.[Product Number] AND MC.[Week Ending Date] = AF.[Week End Date] 
JOIN Table3 SC ON SC.[Product Number] = AF.[Product Number] AND SC.[Week Ending Date] = MC.[Week Ending Date] 
WHERE 
AF.[Product Number] = '96443' AND 
AF.[Week End Date] = '2/4/2017' AND 
MC.[Rout Line Name] = 'Total Cost' AND 
SC.[Rout Line Name] = 'Total Cost' 
GROUP BY 
AF.[Product Number] 
+0

沒有骰子。它仍然重複MC和SC的總和,我可能還需要總結總銷售額和磅數。 – jDave1984

2

我用曼德爾森的臨時表和重新排列的子查詢到的CTE。

CREATE TABLE #Table1 (id INT IDENTITY(1,1), ProdNum INT, WeekEndDate DATETIME, InvChangeCost NUMERIC(18,2), GrossSales NUMERIC(18,2), ProductionLibs NUMERIC(18,2)) 

INSERT INTO #Table1(ProdNum ,WeekEndDate ,InvChangeCost ,GrossSales ,ProductionLibs) 
VALUES(96443, '2/4/2017', -123456, 1000,   2000) 

CREATE TABLE #Table2 (id INT IDENTITY(1,1), ProdNum INT, WeekEndDate DATETIME, MeatCost NUMERIC(18,2)) 

INSERT INTO #Table2(ProdNum ,WeekEndDate ,MeatCost) 
VALUES(96443, '2/4/2017', 5000) 
INSERT INTO #Table2(ProdNum ,WeekEndDate ,MeatCost) 
VALUES(96443, '2/4/2017', 6000) 

CREATE TABLE #Table3 (id INT IDENTITY(1,1), ProdNum INT, WeekEndDate DATETIME, SeasonCost NUMERIC(18,2)) 

INSERT INTO #Table3(ProdNum ,WeekEndDate ,SeasonCost) 
VALUES(96443, '2/4/2017', 1000) 
INSERT INTO #Table3(ProdNum ,WeekEndDate ,SeasonCost) 
VALUES(96443, '2/4/2017', 2000) 

;WITH cteMeatCost 
AS 
(
    SELECT ProdNum, WeekEndDate, SUM(MeatCost) AS MeatCost 
      FROM #Table2 
      GROUP BY ProdNum, WeekEndDate 
) 

,cteSeasonCost 
AS 
(
    SELECT ProdNum, WeekEndDate, SUM(SeasonCost) AS SeasonCost 
      FROM #Table3 
      GROUP BY ProdNum, WeekEndDate 
) 

SELECT t1.* 
     , t2.MeatCost 
     , t3.SeasonCost 
FROM #Table1 t1 
    JOIN cteMeatCost t2 ON 
     t1.ProdNum = t2.ProdNum AND 
     t1.WeekEndDate = t2.WeekEndDate 
    JOIN cteSeasonCost t3 ON 
     t3.ProdNum = t2.ProdNum AND 
     t3.WeekEndDate = t2.WeekEndDate 


DROP TABLE #Table1 
DROP TABLE #Table2 
DROP TABLE #Table3 
+0

明白了。謝謝。 – manderson