递归查询中不可理解的多个相同结果(Oracle)

时间:2015-02-09 12:01:20

标签: sql oracle recursive-query

我仍然遇到一些麻烦的递归查询让我感到困惑 绝对疯了。我会尝试足够具体,但我不能重现我在一个更简单的例子上得到的...因此,如果需要另一个表视图,请询问。

当试图在我的表中获得“路径”后跟一个互连链时,我会得到一些非常奇怪的双重(但看似相同但是)结果。看了之后,我可以注意到这一点:

在没有递归的情况下返回我需要的内容时,我得到:


SELECT  
        instr_id, sensor_id_in, sensor_id_out, date_begin, date_end
FROM    ais_connect
JOIN    ais_sensor ON ais_sensor.sensor_id = ais_connect.sensor_id_in
JOIN    ais_sensor_catalog ON ais_sensor_catalog.sensor_catal_id = ais_sensor.sensor_catal_id
JOIN    ais_instr_catalog ON ais_instr_catalog.instr_catal_id = ais_sensor_catalog.instr_catal_id
WHERE  ((date_begin is null or date_begin < to_date('20150120090025','YYYYMMDDHH24MISS')
AND     (date_end   is null or date_end   >=to_date('20150120090025','YYYYMMDDHH24MISS'))

returns : 

  INSTR_ID SENSOR_ID_IN SENSOR_ID_OUT DATE_BEGIN          DATE_END                                                                                                                                                                           
---------- ------------ ------------- ------------------- -------------------                                                                                                                                                                
        37           37            33 01/03/2012 12:00:00                                                                                                                                                                                    
        31           30             2 10/04/2012 12:00:00                                                                                                                                                                                    
       198          225           223 10/12/2014 09:47:03                                                                                                                                                                                    
        37           40            34 01/03/2012 12:00:00                                                                                                                                                                                    
        39           46           221 14/01/2015 00:00:00                                                                                                                                                                                    
        39           46             9 21/02/2012 15:00:00                                                                                                                                                                                    
        39           46            19 21/02/2012 15:00:00                                                                                                                                                                                    
        39           46             8 21/02/2012 15:00:00                                                                                                                                                                                    
        39           46            20 21/02/2012 15:00:00
        39           46            13 21/02/2012 15:00:00
        39           46            16 21/02/2012 15:00:00
        39           46            15 21/02/2012 15:00:00
        39           46            14 21/02/2012 15:00:00
        39           46            12 21/02/2012 15:00:00
        39           46            21 24/05/2012 12:00:00
        39           46            50 07/08/2014 13:24:00
       181          205           202 15/09/2014 10:00:00
       181          205           203 15/09/2014 10:00:00
        37           41            36 13/03/2012 12:00:00
        37           42            45 24/05/2012 00:00:00
       175          199            93 15/09/2014 10:00:00
       196          223           219 30/11/2014 14:00:00
       167          184           208 15/09/2014 10:00:00
        27           26            47 21/11/2013 11:34:00
        26           25            48 21/11/2013 11:34:00
       165          182           218 05/11/2014 00:00:00
       197          224           220 30/11/2014 14:00:00
       166          183           217 05/11/2014 00:00:00
        41           49             5 21/11/2013 12:34:00

但是当我尝试做的时候:

SELECT  
        level lvl,
        connect_by_root instr_id top,
        connect_by_isleaf is_leaf,
        sys_connect_by_path(sensor_id_in, ';') chain_id
FROM    ais_connect
JOIN    ais_sensor ON ais_sensor.sensor_id = ais_connect.sensor_id_in
JOIN    ais_sensor_catalog ON ais_sensor_catalog.sensor_catal_id = ais_sensor.sensor_catal_id
JOIN    ais_instr_catalog ON ais_instr_catalog.instr_catal_id = ais_sensor_catalog.instr_catal_id
WHERE   (date_begin is null or date_begin   =to_date('20150120090025','YYYYMMDDHH24MISS'))
START WITH sensor_id_out = '219'
CONNECT BY prior sensor_id_in = sensor_id_out
ORDER BY lvl

It returns me :

       LVL        TOP    IS_LEAF CHAIN_ID
---------- ---------- ---------- ----------------
         1        196          0 ;223
         2        196          1 ;223;225
         2        196          1 ;223;225

知道为什么我能得到这条双线?

1 个答案:

答案 0 :(得分:0)

请注意where子句在connect by之后处理。如果这不是所需的行为,请尝试使用connect by - query。

包装where-query
select ...
from ( select ...
       from ...
       where ... )
start with ...
connect by ...
order by ...