に表示しますSUMと、JOINは、私は3つのテーブルを持っている一つのレコード
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
しかし、私はSUM肉と季節にできるようにしたいので、テーブルは次のようになります:
次を返します私は次のステートメントを試してみたが、私は私が欲しい値が返されないの両方和について合計4つのすべてのレコードを取得しています:
Prod # | WE Date | IC Cost | Gross Sales | Prod Lbs | Meat Cost | Season Cost |
96443 2/4/2017 -123456 1000 2000 22000 60000
:
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];
の結果を返すに
希望の結果を得るために、すべてのフィールドを合計する必要がありますか?
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';
しかし、私は前の試行と同じ結果を取得しています。
これ以上のフィールドはありますか?シーズンコストとは何ですか? – manderson
私はかなり多くのフィールドを持っていますが、それらのほとんどはそれぞれのテーブルにユニークです – jDave1984
あなたのクエリは期待通りに機能しますが、96443の製品番号の違いを知る必要があります。 – manderson