如何从S子Select返回正确的总和?
我有一个较大选择的子选择。我想将所有权重以一个订单并显示该总和总和,但是不断获得错误或多个返回的值:
SELECT "Invoice Date", "Invoice Number", "Invoice Currency Code",
(select sum(items.GEWICHTF) from consignment left join items on items.consid=consignment.id group by consignment.orderNo)) "Weight",
FROM order
LEFT JOIN partner ON order.customer=partner.nr
LEFT JOIN consignment ON order.nr=consignment.orderno
LEFT JOIN items ON items.consid=consignment.id
LEFT JOIN transportorder ON consignment.consid=transportorder.consid
WHERE partner.nr=26601102;
我该怎么做?
I have a subselect which is part of a bigger select. I want to SUM all weights for one order and display that sum, but constantly get errors or multiple returned values:
SELECT "Invoice Date", "Invoice Number", "Invoice Currency Code",
(select sum(items.GEWICHTF) from consignment left join items on items.consid=consignment.id group by consignment.orderNo)) "Weight",
FROM order
LEFT JOIN partner ON order.customer=partner.nr
LEFT JOIN consignment ON order.nr=consignment.orderno
LEFT JOIN items ON items.consid=consignment.id
LEFT JOIN transportorder ON consignment.consid=transportorder.consid
WHERE partner.nr=26601102;
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您说的那样,您不知道相关的子查询是什么,然后(在代码中阅读评论):
我知道,您只发布了您真正拥有的一小部分代码,或者可能无法正常工作。
评论您对Alex评论的评论:
不可能的?另一方面,这是可能的 - 仅需要一些键入,即通过子句将所有非聚集列指定到
组中。
它可以正常工作吗?我不知道,这取决于您想要的。
将工作的选项是创建函数来计算结果,然后直接从查询中调用该函数,直接:
这样做,您不需要
>基于
的组(除非那里还有其他聚集体)。退税?上下文切换;可能(并且会)减慢事情的速度。对于小型数据集,您不会注意到不同的。对于大型数据集,您很可能会。
最后,只有一个评论:这是甲骨文,我们通常讨厌 将任何东西封闭在双引号中,并在单词之间放置空间。让前端护理标签。我建议您这样做
,然后(正如我所说的)在前端使用您想要的任何标签。
As you said that you don't know what a correlated subquery is, then (read comments within code):
I understand, you posted just a small piece of code you really have so this may, or may not work.
To comment your comment about what Alex commented:
Impossible? On the other hand, it would be possible - it would just require some more typing, to specify all non-aggregated columns into the
group by
clause.Would it work properly? I have no idea, it depends on what you want.
Option which would work is to create a function that calculates the result and then call that function from your query, directly:
Doing so, you wouldn't need a
GROUP BY
at all (unless there are other aggregates there).Drawback? Context switching; might (and will) slow things down. For small data set, you won't notice the different. For large data set, you most probably will.
Finally, just a comment: this is Oracle, and we usually hate enclosing anything into double quotes and put spaces in between words. Let the front end care about labels. I'd suggest you to
and then (as I said) use any label you want in front end.
如果所有行都在您的外部查询中(无重复),则可以使用
sum
分析函数:If all the rows are in your outer query (without duplicates) then you you can use the
SUM
analytic function: