Oracle“CONNECT BY”语法

时间:2010-08-19 19:49:05

标签: sql oracle connect-by

这是以下问题的分支: Single out duplicates between two result sets

正如通过对这些问题的评论,我正在尝试使用Oracle的特殊“CONNECT BY”语法来实现我的查询。我无法找到关于如何在我的情况下实现语法的任何(清晰)信息。

我的查询:

SELECT pi.compressed_name, pi.phn, to_char(pi.date_of_birth , 'YYYY/MM/DD') as date_of_birth,
  to_char(pe.started_on , 'YYYY/MM/DD' ) as medicare_eligibility_start,
  to_char(pe.ended_on , 'YYYY/MM/DD' ) as medicare_eligibility_end
FROM    medcrtr.forest_node fnpppp,
    medcrtr.forest_node fnppp,
    medcrtr.forest_node fnpp,
    medcrtr.forest_node fnp,
    medcrtr.forest_node fn,
    medcrtr.group_member gm,
    medcrtr.program_eligibility pe,
    person_index pi
WHERE   gm.entity_type_id = 1
    AND fn.source_id = gm.group_id
    AND fn.entity_type_id = 3
    AND fnp.id = fn.parent_id
    AND fnpp.id = fnp.parent_id
    AND fnppp.id = fnpp.parent_id
    AND fnpppp.id = fnppp.parent_id
    AND pe.person_id = gm.source_id
    AND pe.sub_program_id = fnpp.parent_id
    AND pi.person_id = gm.source_id
  AND fnppp.id = 1169
    AND (gm.ended_on >= SYSDATE OR gm.ended_on IS NULL)

有人能指出我正确的方向将其转换为不同的语法吗?

我正在考虑以下几点:

SELECT   pi.compressed_name, pi.phn, to_char(pi.date_of_birth , 'YYYY/MM/DD') as date_of_birth,
    to_char(pe.started_on , 'YYYY/MM/DD' ) as medicare_eligibility_start,
    to_char(pe.ended_on , 'YYYY/MM/DD' ) as medicare_eligibility_end
FROM    medcrtr.forest_node fn,
    group_member gm,
    program_eligibility pe,
    person_index pi
WHERE   gm.entity_type_id = 1
    AND fn.source_id = gm.group_id
    AND fn.entity_type_id = 3
    AND pe.person_id = gm.source_id
    --AND pe.sub_program_id = fnpp.parent_id ???
    AND pi.person_id = gm.source_id
    --AND fnppp.id = 1169 ???
    AND (gm.ended_on >= SYSDATE OR gm.ended_on IS NULL)
CONNECT BY PRIOR fn.id=fn.parent_id

这显然不起作用,我不知道如何整合fnpp的功能......还有什么帮助?

1 个答案:

答案 0 :(得分:1)

我不确定,但我认为你错过了start with条款:

SELECT   pi.compressed_name, pi.phn, to_char(pi.date_of_birth , 'YYYY/MM/DD') as date_of_birth,
    to_char(pe.started_on , 'YYYY/MM/DD' ) as medicare_eligibility_start,
    to_char(pe.ended_on , 'YYYY/MM/DD' ) as medicare_eligibility_end
FROM    medcrtr.forest_node fn,
    group_member gm,
    program_eligibility pe,
    person_index pi
WHERE   gm.entity_type_id = 1
    AND fn.source_id = gm.group_id
    AND fn.entity_type_id = 3
    AND pe.person_id = gm.source_id
    AND pi.person_id = gm.source_id
    AND (gm.ended_on >= SYSDATE OR gm.ended_on IS NULL)
start with fn.id = 1169 /*THis is where the recursion will start */
CONNECT BY PRIOR prior fn.id=fn.parent_id /*specify you want the current node's parent id to match the previous node's id*/

一个很好的参考:http://www.adp-gmbh.ch/ora/sql/connect_by.html