优化 SQL 语句以使用查询,但需要以某种方式引用子查询之外的列
Oracle 有没有办法在 SQL 查询中引用子查询外部的列,或者即使通过不同的方式也能达到这种效果?到目前为止我在网上读到的所有内容都对此没有帮助。
例如(这就是我所追求的事情):
SELECT a.product_code, b.received, b.issued
FROM productinfo a,
(SELECT SUM(qty_received) AS received, SUM(qty_issued) AS issued
FROM productdetail b WHERE b.product_code = a.product_code AND active = 1);
我已经尝试了很多不同的变体/组合,目前我只是收到类似 ORA-00904 的错误:与 WHERE 子句关系相关的标识符无效。
目前,如果我作为单独的查询运行,例如:
SELECT product_code FROM productinfo;
然后对于每条记录:
SELECT SUM(qty_received) AS received, SUM(qty_issued) AS issued FROM productdetail
WHERE product_code = '(specified)' AND active = 1;
这可能需要半个小时以上才能运行 8000 条记录,这简直是愚蠢的。
头发用完了,感谢任何帮助!谢谢。
Is there any way in Oracle within an SQL query to reference a column from within a subquery that is outside of it, or to achieve that effect even if via a different means? Everything I've read on the web so far has just not helped with this.
For example (this is the kind of thing I'm after):
SELECT a.product_code, b.received, b.issued
FROM productinfo a,
(SELECT SUM(qty_received) AS received, SUM(qty_issued) AS issued
FROM productdetail b WHERE b.product_code = a.product_code AND active = 1);
I've tried loads of different variations/combinationsAt the moment I just get errors like ORA-00904: invalid identifier relating to the WHERE clause relationship.
At present if I run as seperate queries, e.g.:
SELECT product_code FROM productinfo;
and then for each of those records:
SELECT SUM(qty_received) AS received, SUM(qty_issued) AS issued FROM productdetail
WHERE product_code = '(specified)' AND active = 1;
This can take over an half an hour to run for 8000 records which is just plain daft.
Running out of hair, any help appreciated!! Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ORA 错误是因为您无法关联派生表/内联视图 - 您需要使用 JOIN 表示法(ANSI-89 或 92)。
假设您确实需要
PRODUCTINFO
表中的列,请使用:如果您想查看 ProductInfo 记录的列表(可能有也可能没有 PRODUCTDETAIL 记录),请使用:
但示例看起来像你可能只需要使用:
The ORA error is because you can't correlate the derived table/inline view - you need to use JOIN notation (ANSI-89 or 92).
Assuming you really need column(s) from the
PRODUCTINFO
table, use:If you want to see a list of the productinfo records, which may or may not have PRODUCTDETAIL records, use:
But the example looks like you might only need to use:
只需在查询内部进行分组并使用内部联接
Just group by inside the query and use an inner join