如何 return 更正 s 子选择的总和?
How to return correct sum from s subselect?
我有一个子select,它是更大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;
我该怎么做?
正如你所说,你不知道什么是相关子查询,那么(阅读代码中的注释):
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;
我知道,您发布的只是一小段您真正拥有的代码,所以这可能有效,也可能无效。
要对亚历克斯评论的内容发表评论:
It would be impossible to group the whole query.
不可能?另一方面,这是可能的——它只需要更多的输入,将所有 non-aggregated 列指定到 group by
子句中。
它能正常工作吗?我不知道,这取决于你想要什么。
工作的选项是创建一个 函数 来计算结果,然后直接从您的查询中调用该函数:
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
这样做,您根本不需要 GROUP BY
(除非那里有其他聚合)。
缺点?上下文切换;可能(并且将会)放慢速度。对于小数据集,您不会注意到差异。对于大型数据集,您很可能会。
最后,只是评论:这是 Oracle,我们通常 讨厌 将任何内容括在双引号中并在单词之间放置空格。让前端关心标签。我建议你
select invoice_date, invoice_currency_code, weight from ...
然后(如我所说)在前端使用任何你想要的标签。
如果所有行都在您的外部查询中(没有重复),那么您可以使用 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;
我有一个子select,它是更大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;
我该怎么做?
正如你所说,你不知道什么是相关子查询,那么(阅读代码中的注释):
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;
我知道,您发布的只是一小段您真正拥有的代码,所以这可能有效,也可能无效。
要对亚历克斯评论的内容发表评论:
It would be impossible to group the whole query.
不可能?另一方面,这是可能的——它只需要更多的输入,将所有 non-aggregated 列指定到 group by
子句中。
它能正常工作吗?我不知道,这取决于你想要什么。
工作的选项是创建一个 函数 来计算结果,然后直接从您的查询中调用该函数:
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
这样做,您根本不需要 GROUP BY
(除非那里有其他聚合)。
缺点?上下文切换;可能(并且将会)放慢速度。对于小数据集,您不会注意到差异。对于大型数据集,您很可能会。
最后,只是评论:这是 Oracle,我们通常 讨厌 将任何内容括在双引号中并在单词之间放置空格。让前端关心标签。我建议你
select invoice_date, invoice_currency_code, weight from ...
然后(如我所说)在前端使用任何你想要的标签。
如果所有行都在您的外部查询中(没有重复),那么您可以使用 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;