1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
WITH src
AS (SELECT 1 id, 'F1' famille, 'nom1' pere, 'nom2' enfant, 'M' sexe FROM DUAL
UNION ALL
SELECT 2 id, 'F1' famille, 'nom2' pere, 'nom3' enfant, 'M' sexe FROM DUAL
UNION ALL
SELECT 3 id, 'F1' famille, 'nom3' pere, 'nom4' enfant, 'F' sexe FROM DUAL
UNION ALL
SELECT 4 id, 'F1' famille, 'nom4' pere, 'nom5' enfant, 'M' sexe FROM DUAL
UNION ALL
SELECT 5 id, 'F2' famille, 'nom11' pere, 'nom12' enfant, 'M' sexe FROM DUAL
UNION ALL
SELECT 6 id, 'F2' famille, 'nom12' pere, 'nom13' enfant, 'F' sexe FROM DUAL
UNION ALL
SELECT 7 id, 'F2' famille, 'nom13' pere, 'nom14' enfant, 'M' sexe FROM DUAL
UNION ALL
SELECT 18 id, 'F3' famille, 'nom21' pere, 'nom22' enfant, 'M' sexe FROM DUAL)
SELECT id_pere
, MIN (pere) KEEP (DENSE_RANK FIRST ORDER BY id_pere, lvl) premier_nom_famille
, MAX (enfant) KEEP (DENSE_RANK LAST ORDER BY id_pere, lvl) dernier_nom_famille
FROM ( SELECT a.*, LEVEL lvl, CONNECT_BY_ROOT a.pere id_pere
FROM src a
CONNECT BY a.pere = PRIOR a.enfant
START WITH a.pere NOT IN (SELECT enfant
FROM src))
GROUP BY id_pere
ORDER BY 1; |
Partager