优化 SQL 语句以使用查询,但需要以某种方式引用子查询之外的列

发布于 2024-10-15 11:54:25 字数 748 浏览 5 评论 0原文

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

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

发布评论

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

评论(2

马蹄踏│碎落叶 2024-10-22 11:54:25

ORA 错误是因为您无法关联派生表/内联视图 - 您需要使用 JOIN 表示法(ANSI-89 或 92)。

假设您确实需要 PRODUCTINFO 表中的列,请使用:

SELECT a.product_code, b.received, b.issued
  FROM PRODUCTINFO a
  JOIN (SELECT t.product_code,
               SUM(t.qty_received) AS received, 
               SUM(t.qty_issued) AS issued
          FROM PRODUCTDETAIL t 
         WHERE t.active = 1
      GROUP BY t.produce_code) b ON b.product_code = a.product_code

如果您想查看 ProductInfo 记录的列表(可能有也可能没有 PRODUCTDETAIL 记录),请使用:

   SELECT a.product_code, b.received, b.issued
     FROM PRODUCTINFO a
LEFT JOIN (SELECT t.product_code,
                  SUM(t.qty_received) AS received, 
                  SUM(t.qty_issued) AS issued
             FROM PRODUCTDETAIL t 
            WHERE t.active = 1
         GROUP BY t.produce_code) b ON b.product_code = a.product_code

但示例看起来像你可能只需要使用:

 SELECT t.product_code,
        SUM(t.qty_received) AS received, 
        SUM(t.qty_issued) AS issued
   FROM PRODUCTDETAIL t 
   WHERE t.active = 1
GROUP BY t.produce_code

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:

SELECT a.product_code, b.received, b.issued
  FROM PRODUCTINFO a
  JOIN (SELECT t.product_code,
               SUM(t.qty_received) AS received, 
               SUM(t.qty_issued) AS issued
          FROM PRODUCTDETAIL t 
         WHERE t.active = 1
      GROUP BY t.produce_code) b ON b.product_code = a.product_code

If you want to see a list of the productinfo records, which may or may not have PRODUCTDETAIL records, use:

   SELECT a.product_code, b.received, b.issued
     FROM PRODUCTINFO a
LEFT JOIN (SELECT t.product_code,
                  SUM(t.qty_received) AS received, 
                  SUM(t.qty_issued) AS issued
             FROM PRODUCTDETAIL t 
            WHERE t.active = 1
         GROUP BY t.produce_code) b ON b.product_code = a.product_code

But the example looks like you might only need to use:

 SELECT t.product_code,
        SUM(t.qty_received) AS received, 
        SUM(t.qty_issued) AS issued
   FROM PRODUCTDETAIL t 
   WHERE t.active = 1
GROUP BY t.produce_code
偏爱自由 2024-10-22 11:54:25

只需在查询内部进行分组并使用内部联接

SELECT a.product_code, b.received, b.issued
FROM productinfo a
INNER JOIN
 (SELECT product_code, SUM(qty_received) AS received, SUM(qty_issued) AS issued
  FROM productdetail
  WHERE active = 1
  GROUP BY product_code) b on b.product_code = a.product_code

Just group by inside the query and use an inner join

SELECT a.product_code, b.received, b.issued
FROM productinfo a
INNER JOIN
 (SELECT product_code, SUM(qty_received) AS received, SUM(qty_issued) AS issued
  FROM productdetail
  WHERE active = 1
  GROUP BY product_code) b on b.product_code = a.product_code
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文