先前为DB2连接

时间:2013-04-24 05:36:52

标签: database oracle db2

我遇到分层查询问题,以下查询在Oracle中有效,但在DB2

中没有

如果我们从芝加哥开始旅程,它将检索目的地。

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

任何人都可以帮助我,我们如何在DB2中编写查询

先谢谢 拉杰什

3 个答案:

答案 0 :(得分: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手头)

答案 1 :(得分: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_with_no_name

答案 2 :(得分:0)

您没有提到正在运行的操作系统。

在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。也许在将来的版本中。