具有相关子查询的 SQL Sum()

发布于 2024-12-03 12:42:25 字数 466 浏览 0 评论 0原文

我有两个表:基金和项目。 Items 有一个名为fundID 的字段,与基金表的ID 相关。物品也有价格字段。

下面是两个表的简化示例:

FUND
ID    fundName
1     maintenance
2     books
3     development
4     media


ITEMS
ID    price    fundID
1     $10        2
2     $20        4
3     $5         4
4     $8         1
5     $10        3
6     $12        4

我想创建一个查询,该查询将提供每个基金的名称以及通过fundID 字段连接到该基金的项目的所有价格总和。我尝试了几种方法,例如在 SUM() 和 WHERE 中使用相关子查询,但出现错误。

如果有人能指出我正确的方向,我将不胜感激。

I have two tables: fund and items. Items has a field called fundID that is related to the ID of the fund table. Items also has a price field.

Here's a simplified example of the two tables:

FUND
ID    fundName
1     maintenance
2     books
3     development
4     media


ITEMS
ID    price    fundID
1     $10        2
2     $20        4
3     $5         4
4     $8         1
5     $10        3
6     $12        4

I want to create a query that will give me the name of each fund and the sum of all prices for items that are connected to that fund through the fundID field. I have tried several methods such as having a correlated subquery within the SUM() and also within the WHERE, and I am getting errors.

If someone could point me in the right direction, I'd appreciate it.

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

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

发布评论

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

评论(4

‖放下 2024-12-10 12:42:26

考虑到您的表结构,没有充分的理由这样做,但是问题确实询问了子查询并且表结构得到了简化。

因此,这将给出与直接 JOIN/GROUP BY 相同的结果。

SELECT f.fundname, 
       coalesce(i.price,0) price
FROM   fund f 
       LEFT JOIN (SELECT fundid, 
                          SUM(price) price 
                   FROM   items i 
                   GROUP  BY fundid) i 
         ON f.fundid = i.fundid 

请注意,如果您希望没有项目的资金为 0,则可以使用 COALESCE/LEFT JOIN。

There's no good reason to do it this way given your table structure however the question does ask about a sub query and the table structure is simplified.

So this will give the same results as straightfoward JOIN/GROUP BY

SELECT f.fundname, 
       coalesce(i.price,0) price
FROM   fund f 
       LEFT JOIN (SELECT fundid, 
                          SUM(price) price 
                   FROM   items i 
                   GROUP  BY fundid) i 
         ON f.fundid = i.fundid 

Note the COALESCE/LEFT JOIN is there in case you want 0 for funds that have no items.

故人的歌 2024-12-10 12:42:26

试试这个

select fundName,sum(price) as  TotPrice
from Funds f
join Items g on g.id=f.id
group by f.fundName

Try this

select fundName,sum(price) as  TotPrice
from Funds f
join Items g on g.id=f.id
group by f.fundName
把梦留给海 2024-12-10 12:42:26
SELECT f.fundid, f.fundName, SUM(price)
FROM Fund f 
   INNER JOIN ITEMS i ON i.fundId = f.fundId
GROUP BY f.FUNDID

如果您有很多资金,基金 ID 可能更好按分组

SELECT f.fundid, f.fundName, SUM(price)
FROM Fund f 
   INNER JOIN ITEMS i ON i.fundId = f.fundId
GROUP BY f.FUNDID

If you have a lot of funds fund Id is probably better to group by

泅人 2024-12-10 12:42:25

试试这个:

SELECT f.fundname,
       SUM(i.price)
FROM   fund f
       JOIN items i
         ON i.fundid = f.id
GROUP  BY f.fundname  

Try this :

SELECT f.fundname,
       SUM(i.price)
FROM   fund f
       JOIN items i
         ON i.fundid = f.id
GROUP  BY f.fundname  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文