1

これは単純かもしれませんが、他のロジックを適用する必要があります:

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;

次のような単純な出力:

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

3番目の列に使用した内側に最も外側の値を使用する必要があります

EDITING -- より多くのデータを含むサンプル:

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;

次のような出力が期待されます。

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

助けてくれてありがとう。

4

2 に答える 2

2

outer_q.col 参照を 2 レベル下にプッシュしようとしたため、内側のクエリは失敗します。相関クエリは 1 レベルだけ下に移動します

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

于 2013-10-04T08:22:12.553 に答える