从sql中检索总金额

发布于 2024-10-20 13:47:36 字数 551 浏览 0 评论 0原文

以下查询有什么问题:

SELECT wbCode||yr, CAST(coalesce(Cons,0) AS FLOAT), wbCode
FROM Commodities
Where CCode in (2611,2513,2961) Group by wbCode||yr

例如,我希望上面的查询返回给定年份(USA1990)中每个 wbCode 的总和;但上面的代码并没有总结;它正在检索与以下内容相同的数据:

SELECT wbCode||yr, CAST(coalesce(Cons,0) AS FLOAT), wbCode 
FROM Commodities
Where CCode in (2611) Group by wbCode||yr

SELECT wbCode||yr, CAST(coalesce(Cons,0) AS FLOAT), wbCode 
FROM Commodities Where CCode in (2513,2961)
Group by wbCode||yr

如何求和?

非常感谢您的帮助。

What is the problem with the following query:

SELECT wbCode||yr, CAST(coalesce(Cons,0) AS FLOAT), wbCode
FROM Commodities
Where CCode in (2611,2513,2961) Group by wbCode||yr

I want the query above to return the sums for every wbCode in a given yr (USA1990), for example; but the code above is not summing up; it is retrieving the same data as:

SELECT wbCode||yr, CAST(coalesce(Cons,0) AS FLOAT), wbCode 
FROM Commodities
Where CCode in (2611) Group by wbCode||yr

SELECT wbCode||yr, CAST(coalesce(Cons,0) AS FLOAT), wbCode 
FROM Commodities Where CCode in (2513,2961)
Group by wbCode||yr

How can I get it to sum?

Thank you so much for your help.

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

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

发布评论

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

评论(3

醉南桥 2024-10-27 13:47:36

为什么||?只需将它们分别分组

SELECT wbCode, yr, SUM(CAST(coalesce(Cons,0) AS FLOAT)), wbCode
FROM Commodities
Where CCode in (2611,2513,2961)
Group by wbCode, yr

或者

SELECT wbCode||yr, SUM(CAST(coalesce(Cons,0) AS FLOAT)), wbCode
FROM Commodities
Where CCode in (2611,2513,2961)
Group by wbCode, yr

Why || ? Just group by them separately

SELECT wbCode, yr, SUM(CAST(coalesce(Cons,0) AS FLOAT)), wbCode
FROM Commodities
Where CCode in (2611,2513,2961)
Group by wbCode, yr

Or

SELECT wbCode||yr, SUM(CAST(coalesce(Cons,0) AS FLOAT)), wbCode
FROM Commodities
Where CCode in (2611,2513,2961)
Group by wbCode, yr
丿*梦醉红颜 2024-10-27 13:47:36

为什么不使用 SUM 函数来汇总结果?

Why aren't you using SUM function to aggregate results?

吹梦到西洲 2024-10-27 13:47:36

这个怎么样:

  SELECT wbCode
  ,      yr
  ,      SUM(CAST(coalesce(Cons,0) AS FLOAT))
    FROM Commodities
   WHERE CCode in (2611,2513,2961) 
GROUP BY wbCode
,        yr;

How about this:

  SELECT wbCode
  ,      yr
  ,      SUM(CAST(coalesce(Cons,0) AS FLOAT))
    FROM Commodities
   WHERE CCode in (2611,2513,2961) 
GROUP BY wbCode
,        yr;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文