Tuesday 7 January 2014

Hierarchical Queries : CONNECT BY

--Query with all the childs
 
         SELECT fh.parent_flex_value,
          fh.child_flex_value_low low_value,
          fh.child_flex_value_high high_value,
          fh.range_attribute --, level
        FROM fnd_flex_value_norm_hierarchy fh
        WHERE fh.flex_value_set_id =
          (SELECT fvs.flex_value_set_id
           FROM fnd_flex_value_sets fvs
           WHERE fvs.flex_value_set_name = p_value_set
           AND rownum = 1)
        START WITH fh.parent_flex_value = p_value CONNECT BY PRIOR fh.child_flex_value_low = fh.parent_flex_value;




 
--Query with all the child ranges
 
        WITH accounts AS
              (SELECT temp.low_value,
                      temp.high_value
               FROM (SELECT fh.parent_flex_value,
                            fh.child_flex_value_low low_value,
                            fh.child_flex_value_high high_value,
                            fh.range_attribute
                      FROM fnd_flex_value_norm_hierarchy fh,
                           fnd_flex_value_sets fvs
                      WHERE fh.flex_value_set_id = fvs.flex_value_set_id
                        AND fvs.flex_value_set_name = 'GlobalCostCentre'
                 START WITH fh.parent_flex_value = :p_value
           CONNECT BY PRIOR fh.child_flex_value_low = fh.parent_flex_value) temp)
        SELECT fv.flex_value,
                 fv.summary_flag
          FROM fnd_flex_values fv,
               fnd_flex_value_sets fvs,
               accounts acc
         WHERE fv.flex_value_set_id = fvs.flex_value_set_id
           AND fvs.flex_value_set_name = 'GlobalCostCentre'
           AND fv.flex_value BETWEEN acc.low_value
                                 AND acc.high_value
           AND fv.enabled_flag = 'Y'
           AND SYSDATE BETWEEN NVL(fv.start_date_active,   SYSDATE)
                           AND NVL(fv.end_date_active,   SYSDATE)
        UNION
        SELECT fv.flex_value,
                  fv.summary_flag
          FROM fnd_flex_value_sets fvs,
               fnd_flex_values fv
         WHERE fvs.flex_value_set_name = 'GlobalCostCentre' --- GlobalAccount
           AND fvs.flex_value_set_id = fv.flex_value_set_id
           AND fv.flex_value = :p_value
           AND fv.enabled_flag = 'Y'
           AND SYSDATE BETWEEN NVL(fv.start_date_active,   SYSDATE)
           AND NVL(fv.end_date_active,   SYSDATE);

1 comment: