Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with mz(idx,name,a,b,c) as (
- select 1,'Балашиха', 1, 2, 3
- union all select 2,'Балашиха', 4, 5, 6
- union all select 3,'Калач', 7, 7, 7
- ),
- mo(idx,name,d,e,f) as (
- select 1,'Балашиха', 1, 2, 3
- union all select 2,'Реутов', 4, 5, 6
- union all select 3,'Балашиха', 7, 8, 9
- ),
- mt(idx,name,g,h) as (
- select 1,'Балашиха', 1, 2, 3
- union all select 2,'Реутов', 4, 5, 6
- union all select 3,'Химки', 7, 8, 9
- ),
- mz_ranked as (
- select row_number() over (partition by name) as rn,
- *
- from mz
- ),
- mo_ranked as (
- select row_number() over (partition by name) as rn,
- *
- from mo
- ),
- mt_ranked as (
- select row_number() over (partition by name) as rn,
- *
- from mt
- ),
- mz_mo as (
- select row_number() over (partition by coalesce(mz.name, mo.name)) as rn,
- row_number() over () as idx,
- coalesce(mz.name, mo.name) as name,
- mz.a,
- mz.b,
- mz.c,
- mo.d,
- mo.e,
- mo.f
- from mz_ranked as mz
- full join mo_ranked mo on mo.name = mz.name and mo.rn = mz.rn
- ),
- mz_mo_mt as (
- select row_number() over (partition by coalesce(p.name, mt.name)) as rn,
- row_number() over () as idx,
- coalesce(p.name, mt.name) as name,
- p.a,
- p.b,
- p.c,
- p.d,
- p.e,
- p.f,
- mt.g,
- mt.h
- from mz_mo p
- full join mt_ranked mt on mt.name = p.name and mt.rn = p.rn
- )
- select *
- from mz_mo_mt
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement