我有一個分層查詢一個問題,下面的查詢工作在Oracle中,而不是在DB2連接事先對DB2
它將檢索它可以是可能的,如果我們開始從芝加哥之旅目的地..
SELECT origin, departure, arrival
FROM schema001."FLIGHTS"
START WITH departure = 'Chicago'
CONNECT BY PRIOR arrival = departure;
任何一個可以幫助我,怎樣才能編寫查詢DB2中
在此先感謝 拉傑什
我有一個分層查詢一個問題,下面的查詢工作在Oracle中,而不是在DB2連接事先對DB2
它將檢索它可以是可能的,如果我們開始從芝加哥之旅目的地..
SELECT origin, departure, arrival
FROM schema001."FLIGHTS"
START WITH departure = 'Chicago'
CONNECT BY PRIOR arrival = departure;
任何一個可以幫助我,怎樣才能編寫查詢DB2中
在此先感謝 拉傑什
您可以使用遞歸公用表表達式。
事情是這樣的:
with ftree (origin, departure, arrival) as (
select origin,
departure,
arrival
from flights
where departure = 'Chicago'
union all
select c.origin,
c.departure,
c.arrival
from flights c
join ftree p on c.arrival = p.departure
)
select *
from ftree;
(未測試,不手頭有DB2現在)
乾杯....我得到了解決, 工作的解決方案是.. ..
WITH rajesh(departure, arrival) AS
(
select departure, arrival from ALERTS_TEST.flights where departure = 'Chicago'
UNION ALL
select nplus1.departure, nplus1.arrival from ALERTS_TEST.flights as nplus1, rajesh
WHERE rajesh.arrival = nplus1.departure
)
SELECT departure, arrival FROM rajesh;
我已經檢查了DB2 V9.7和SQLServer上面查詢2005年 它的正常工作...... 感謝烏拉圭回合的幫助a_horse_w ith_no_name
您沒有提及運行哪個OS DB2。
運行在IBM i OS 7.1上的DB2 for i 確實具有hierarchical queries,但語法略有不同。
SELECT origin, departure, arrival
FROM schema001.FLIGHTS
START WITH departure = 'Chicago'
CONNECT BY arrival = PRIOR departure;
不幸的是,我不認爲這將適用於DB2 for LUW或z/OS。也許在未來的版本中。
感謝您的幫助,a_horse_with_no_name – user840477 2013-04-24 10:10:26