2013-04-24 78 views
0

我有一个分层查询一个问题,下面的查询工作在Oracle中,而不是在DB2连接事先对DB2

它将检索它可以是可能的,如果我们开始从芝加哥之旅目的地..

SELECT origin, departure, arrival 
FROM schema001."FLIGHTS" 
START WITH departure = 'Chicago' 
CONNECT BY PRIOR arrival = departure; 

任何一个可以帮助我,怎样才能编写查询DB2中

在此先感谢 拉杰什

回答

1

您可以使用递归公用表表达式。

事情是这样的:

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现在)

+0

感谢您的帮助,a_horse_with_no_name – user840477 2013-04-24 10:10:26

1

干杯....我得到了解决, 工作的解决方案是.. ..

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

0

您没有提及运行哪个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。也许在未来的版本中。