1

This might be a simple but I need to apply the logic in other:

WITH t(col) AS (
SELECT 1 FROM dual
UNION SELECT 2 FROM dual
UNION SELECT 3 FROM dual
UNION SELECT 4 FROM dual
UNION SELECT 5 FROM dual
)
SELECT col , --- will works as usual
(SELECT col FROM t WHERE col  = outer_q.col) new_col,  --working as well
(
SELECT sum (latest_col)
from
(
SELECT col latest_col FROM t  WHERE col  = outer_q.col 
UNION ALL
SELECT col FROM t WHERE col   = outer_q.col
)
)newest_col   -- need to get an output "4"
from t outer_q where col  = 2;

An simple output like:

       COL    NEW_COL NEWEST_COL
---------- ---------- ----------
         2          2          4 

I just need to use the outer most value to the inner I used for the third column

EDITING-- sample with more data:

WITH 
t(col) AS
     ( SELECT 1 FROM dual
     UNION
     SELECT 2 FROM dual
     UNION
     SELECT 3 FROM dual
     UNION
     SELECT 4 FROM dual
     UNION
     SELECT 5 FROM dual
     ),
t1(amount, col) AS
     (SELECT 100 , 2 FROM dual
     UNION
     SELECT 200, 3 FROM dual
     )
SELECT col,
     (SELECT col FROM t WHERE col = outer_q.col
     ) new_col,
     (SELECT SUM(x)
     FROM
          (SELECT col x FROM t
          UNION ALL
          SELECT amount x FROM t1
          )
     WHERE col = outer_q.col
     ) newest_col -- gives 315 as it takes whole `SUM`
FROM t outer_q
WHERE col = 2;

An output is expected like:

       COL    NEW_COL NEWEST_COL
---------- ---------- ----------
         2          2        102

Thanks in advance for any help.

4

2 回答 2

2

内部查询失败,因为您尝试将 outer_q.col 引用向下推两级。相关查询仅下降 1 级

参考:http ://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1853075500346799932

于 2013-10-04T08:22:12.553 回答
2

好吧,如果你重构 a but 你的查询,你可以:

WITH t(col) AS (
  SELECT 1 FROM dual
  UNION SELECT 2 FROM dual
  UNION SELECT 3 FROM dual
  UNION SELECT 4 FROM dual
  UNION SELECT 5 FROM dual
)
SELECT col,
       (SELECT col FROM t WHERE col  = outer_q.col) new_col,
       (SELECT sum (latest_col)
        from
        (
          SELECT col latest_col FROM t 
          UNION ALL
          SELECT col FROM t
        ) x
        where x.latest_col = outer_q.col
       ) newest_col   -- need to get an output "4"
from t outer_q where col = 2;

这是可能的,因为outer_q现在在where子查询的子句中。它以前在子子查询中使用过(带有 的那个UNION ALL),而这个隐藏了它。

为了让事情更清楚,现在我们有类似的东西:

with t as (...)
select col,
       (SELECT col FROM t WHERE col  = outer_q.col) new_col,
       (SELECT col FROM (Something more complex) WHERE ... = outer_q.col) new_col,
from t outer_q where col = 2;

因此,我们现在拥有相同级别的“内部性”。

编辑:要回答更新的问题,需要进行一些调整:

WITH t(col) AS
(
  SELECT 1 FROM dual
  UNION
  SELECT 2 FROM dual
  UNION
  SELECT 3 FROM dual
  UNION
  SELECT 4 FROM dual
  UNION
  SELECT 5 FROM dual
),
t1(amount, col) AS
(
  SELECT 100, 2 FROM dual
  UNION
  SELECT 200, 3 FROM dual
)
SELECT col,
     (SELECT col FROM t WHERE col = outer_q.col) new_col,
     (SELECT SUM(amount)
      FROM
          (SELECT col, col amount FROM t  -- row is (1, 1), then (2, 2) etc
           UNION ALL
           SELECT col, amount FROM t1     -- row is (2, 100), then (3, 200) etc
          )
      WHERE col = outer_q.col
     ) newest_col -- gives 102 as it takes whole `SUM`
FROM t outer_q
WHERE col = 2;

要理解的部分在最里面的查询中:您想要对列和金额值求和,因此您重复该col值,就好像它是一个金额一样。

获得相同结果(我猜性能更高)的另一种方法是colamount同一行求和:

WITH t(col) AS
(
  SELECT 1 FROM dual
  UNION
  SELECT 2 FROM dual
  UNION
  SELECT 3 FROM dual
  UNION
  SELECT 4 FROM dual
  UNION
  SELECT 5 FROM dual
),
t1(amount, col) AS
(
  SELECT 100, 2 FROM dual
  UNION
  SELECT 200, 3 FROM dual
)
SELECT col,
     (SELECT col FROM t WHERE col = outer_q.col) new_col,
     (SELECT SUM(all_amount)
      FROM
          (SELECT col, col + amount all_amount FROM t1)
      WHERE col = outer_q.col
     ) newest_col -- gives 315 as it takes whole `SUM`
FROM t outer_q
WHERE col = 2;
于 2013-10-04T08:23:32.237 回答