【问题标题】:SUM and JOIN to display in one recordSUM 和 JOIN 显示在一条记录中
【发布时间】:2017-02-15 16:43:34
【问题描述】:

我有三个表:Table1 (AF)、Table2 (MC) 和 Table3 (SC)

我目前对这些表有这样的查询:

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

我已经尝试了以下语句,但是我得到了两个 Sum 的所有四条记录的总和,这不是我想要返回的值:

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

我是否需要对所有字段求和才能获得所需的结果?

更新:

我尝试过这样的说法:

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';

但是,我得到的结果与之前的尝试相同。

【问题讨论】:

  • 您还有字段吗?什么是季节成本?
  • 我还有很多字段,但大多数都是各自表独有的
  • 您的查询按预期工作,但需要知道 96443 个产品编号之间的区别......表 3 中的内容。
  • 应该是同一个产品号。我需要为每个表指定产品编号为 X 吗?

标签: sql-server join sum


【解决方案1】:

使用这个例子并尝试这样的事情......

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

【讨论】:

  • 这与我提出的使用 CTE 代替 JOIN 中的子查询的解决方案非常相似。
  • @digital.aaron 给我们看。我不使用很多 CTE,希望看到。
  • 它使用相同的想法。找到肉类成本和季节成本的小计,然后将它们加入最终的 SELECT 中。我喜欢 CTE,因为它们往往更容易阅读。
  • 这行得通,除非我必须从 SQL Server 上已经存在的表中插入值.....这实用吗?
  • 等等!我在两个子查询中留下了一个次要的 WHERE 并且它有效!非常感谢您和 @digital.aaron 的帮助!
【解决方案2】:

我使用 Manderson 的临时表并将子查询重新排列为 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

【讨论】:

  • 知道了。谢谢。
【解决方案3】:

你可以试试这个……

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]

【讨论】:

  • 没有骰子。它仍然重复了 MC 和 SC 的总和,我可能还需要总结总销售额和英镑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
  • 2012-10-29
相关资源
最近更新 更多