SQL:用总和连接 3 个表,过滤非聚合列

发布于 2025-01-08 17:47:15 字数 600 浏览 3 评论 0原文

我有以下表格:

    Plant:
ID      Name
1       Plant1
2       Plant2
......

   Rejects:
PlantID Quantity  Date
1       20        01/02/2012
1       3         02/02/2012
2       30        03/02/2012
.....

  Parts
PlantID Quantity  Date
1       300       01/02/2012
2       500       01/02/2012
1       600       02/02/2012
.......

我正在尝试将这三个表连接起来,以便在两个日期之间获得每个工厂的零件和拒绝的总和:

Plant   Parts   Rejects
Plant1       900     23
Plant2       500     30
.....

我尝试过连接,它只是将总和相乘,并且我尝试过子查询,但会成功不允许我使用日期过滤器,因为它没有在 group by 子句中使用。

有人可以帮忙吗?

I have the following tables:

    Plant:
ID      Name
1       Plant1
2       Plant2
......

   Rejects:
PlantID Quantity  Date
1       20        01/02/2012
1       3         02/02/2012
2       30        03/02/2012
.....

  Parts
PlantID Quantity  Date
1       300       01/02/2012
2       500       01/02/2012
1       600       02/02/2012
.......

I'm trying to join the three so that I have a sum of parts and rejects per plant between two dates:

Plant   Parts   Rejects
Plant1       900     23
Plant2       500     30
.....

I've tried joins, which just multiplies the sums and I've tried subqueries that won't let me use a date filter because it's not used in the group by clause.

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风夜微凉 2025-01-15 17:47:15
declare @startDate datetime, @endDate datetime
select @startDate = '1/1/2012', @endDate = '2/1/2012'

select
    p.Name as Plant,

    (select sum(Quantity) 
     from Parts 
     where PlantID = p.ID and date between @startDate and @endDate) as Parts,

    (select sum(Quantity) 
     from Rejects 
     where PlantID = p.ID and date between @startDate and @endDate) as Rejects
from
    Plant p
where
    p.endDate is null
declare @startDate datetime, @endDate datetime
select @startDate = '1/1/2012', @endDate = '2/1/2012'

select
    p.Name as Plant,

    (select sum(Quantity) 
     from Parts 
     where PlantID = p.ID and date between @startDate and @endDate) as Parts,

    (select sum(Quantity) 
     from Rejects 
     where PlantID = p.ID and date between @startDate and @endDate) as Rejects
from
    Plant p
where
    p.endDate is null
伊面 2025-01-15 17:47:15

这就能解决问题:

select p.Name as Plant,
       SUM(t.Quantity) as Parts,
       SUM(r.Quantity) as Rejects
from Plant p
inner join Rejects r on p.ID = r.PlantID
inner join Parts t on p.ID = PlantID
group by p.Name

您可以使用ID 在所有表之间进行INNER JOIN。之后,您只需要 PartsRejects 中的 QuantitiesSUM 即可。为此,您需要使用GROUP BY

This will do the trick:

select p.Name as Plant,
       SUM(t.Quantity) as Parts,
       SUM(r.Quantity) as Rejects
from Plant p
inner join Rejects r on p.ID = r.PlantID
inner join Parts t on p.ID = PlantID
group by p.Name

You make the INNER JOIN between all tables using ID. Afterwards you just need the SUM of the Quantities in Parts and Rejects. For that you need to use the GROUP BY.

玉环 2025-01-15 17:47:15

以下内容应该有效(使用预言机):

SELECT NVL(p.plantid, r.plantid), NVL(parts,0), NVL(rejects,0)
FROM 
    (SELECT plantid, sum(quantity) as parts
    FROM parts
    WHERE date BETWEEN a AND b) p
        FULL JOIN
    (SELECT plantid, sum(quantity) as rejects
    FROM rejects
    WHERE date BETWEEN a and b) r
       ON p.plantid = r.plantid

请注意,这不会使工厂没有废品或零件。

The following should work (using oracle):

SELECT NVL(p.plantid, r.plantid), NVL(parts,0), NVL(rejects,0)
FROM 
    (SELECT plantid, sum(quantity) as parts
    FROM parts
    WHERE date BETWEEN a AND b) p
        FULL JOIN
    (SELECT plantid, sum(quantity) as rejects
    FROM rejects
    WHERE date BETWEEN a and b) r
       ON p.plantid = r.plantid

Note that this will not get plants with no rejects or parts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文