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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
with personne as
(
select 1 as id, 'Pierre' as nom from dual
union all
select 2, 'Paul' from dual
union all
select 3, 'Jacques' from dual
union all
select 4, 'Pascal' from dual
),
frere as
(
select 1 as per_id, 12 as age, 'Blond' as cheveux from dual
union all
select 1, 15, 'Brun' from dual
union all
select 2, 12, 'Blond' from dual
union all
select 2, 15, 'Brun' from dual
union all
select 3, 12, 'Blond' from dual
union all
select 3, 17, 'Chatain' from dual
union all
select 4, 12, 'Blond' from dual
union all
select 4, 17, 'Chatain' from dual
union all
select 4, 18, 'Chatain' from dual
),
t as
(
select f.per_id id, f.age, f.cheveux
, count(*) over (partition by f.per_id) frere_count
from frere f
),
t1 as
(
select distinct f1.id id1, f2.id id2
from t f1, t f2
where f1.frere_count = f2.frere_count
and f1.frere_count =
(
select count(*)
from t f3, t f4
where f3.id = f1.id
and f4.id = f2.id
and f3.age = f4.age
and f3.cheveux = f4.cheveux
)
),
t2 as
(
select id1 id, min(id2) group_id
from t1
group by id1
)
select p.id, p.nom, t2.group_id, f.age, f.cheveux
from personne p, frere f, t2
where p.id = t2.id
and p.id = f.per_id
ID NOM GROUP_ID AGE CHEVEUX
-- ------- -------- --- -------
1 Pierre 1 12 Blond
1 Pierre 1 15 Brun
2 Paul 1 12 Blond
2 Paul 1 15 Brun
3 Jacques 3 12 Blond
3 Jacques 3 17 Chatain
4 Pascal 4 12 Blond
4 Pascal 4 17 Chatain
4 Pascal 4 18 Chatain |
Partager