如何从S子Select返回正确的总和?

发布于 2025-01-31 07:24:05 字数 561 浏览 5 评论 0原文

我有一个较大选择的子选择。我想将所有权重以一个订单并显示该总和总和,但是不断获得错误或多个返回的值:

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 技术交流群。

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

发布评论

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

评论(2

爱情眠于流年 2025-02-07 07:24:05

正如您说的那样,您不知道相关的子查询是什么,然后(在代码中阅读评论):

SELECT "Invoice Date",
       "Invoice Number",
       "Invoice Currency Code",
       --
       -- this is a correlated subquery; it puts ITEMS table to relation with ...
       (SELECT SUM (items.gewichtf)
          FROM items 
         WHERE items.consid = consignment.id) "Weight"
  FROM t_order
       LEFT JOIN partner ON order.customer = partner.nr
       LEFT JOIN consignment ON order.nr = consignment.orderno  --> ... the CONSIGNMENT table
       LEFT JOIN items ON items.consid = consignment.id
       LEFT JOIN transportorder ON consignment.consid = transportorder.consid
 WHERE partner.nr = 26601102;

我知道,您只发布了您真正拥有的一小部分代码,或者可能无法正常工作。


评论您对Alex评论的评论:

不可能对整个查询进行分组。

不可能的?另一方面,这是可能的 - 仅需要一些键入,即通过子句将所有非聚集列指定到组中。

它可以正常工作吗?我不知道,这取决于您想要的。

工作的选项是创建函数来计算结果,然后直接从查询中调用该函数,直接:

CREATE OR REPLACE FUNCTION f_weight (par_id IN items.consid%TYPE)
   RETURN NUMBER
IS
   retval  NUMBER;
BEGIN
   SELECT SUM (i.gewichtf)
     INTO retval
     FROM items i
    WHERE i.consid = par_id;

   RETURN retval;
END;
/

SELECT "Invoice Date",
       "Invoice Number",
       "Invoice Currency Code",
       --
       f_weight (consignemnt.id) "Weight"               --> here
  FROM t_order
       LEFT JOIN partner ON order.customer = partner.nr

这样做,您不需要>基于的组(除非那里还有其他聚集体)。

退税?上下文切换;可能(并且会)减慢事情的速度。对于小型数据集,您不会注意到不同的。对于大型数据集,您很可能会。


最后,只有一个评论:这是甲骨文,我们通常讨厌 将任何东西封闭在双引号中,并在单词之间放置空间。让前端护理标签。我建议您这样做

select invoice_date, invoice_currency_code, weight from ...

,然后(正如我所说的)在前端使用您想要的任何标签。

As you said that you don't know what a correlated subquery is, then (read comments within code):

SELECT "Invoice Date",
       "Invoice Number",
       "Invoice Currency Code",
       --
       -- this is a correlated subquery; it puts ITEMS table to relation with ...
       (SELECT SUM (items.gewichtf)
          FROM items 
         WHERE items.consid = consignment.id) "Weight"
  FROM t_order
       LEFT JOIN partner ON order.customer = partner.nr
       LEFT JOIN consignment ON order.nr = consignment.orderno  --> ... the CONSIGNMENT table
       LEFT JOIN items ON items.consid = consignment.id
       LEFT JOIN transportorder ON consignment.consid = transportorder.consid
 WHERE partner.nr = 26601102;

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:

It would be impossible to group the whole query.

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:

CREATE OR REPLACE FUNCTION f_weight (par_id IN items.consid%TYPE)
   RETURN NUMBER
IS
   retval  NUMBER;
BEGIN
   SELECT SUM (i.gewichtf)
     INTO retval
     FROM items i
    WHERE i.consid = par_id;

   RETURN retval;
END;
/

SELECT "Invoice Date",
       "Invoice Number",
       "Invoice Currency Code",
       --
       f_weight (consignemnt.id) "Weight"               --> here
  FROM t_order
       LEFT JOIN partner ON order.customer = partner.nr

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

select invoice_date, invoice_currency_code, weight from ...

and then (as I said) use any label you want in front end.

凌乱心跳 2025-02-07 07:24:05

如果所有行都在您的外部查询中(无重复),则可以使用sum分析函数:

SELECT "Invoice Date",
       "Invoice Number",
       "Invoice Currency Code",
       SUM(items.GEWICHTF) OVER (PARTITION BY consignment.orderNo) AS "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;

If all the rows are in your outer query (without duplicates) then you you can use the SUM analytic function:

SELECT "Invoice Date",
       "Invoice Number",
       "Invoice Currency Code",
       SUM(items.GEWICHTF) OVER (PARTITION BY consignment.orderNo) AS "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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文