Data Versioning Methods
Method: 1 (Joins & UNION ALL)
select * from
(
(select fawad_id, fawad_value from
(select fawad_id, fawad_value, nazir_id, nazir_value from fawad left outer join nazir on fawad_id = nazir_id)
where nazir_id is NULL)
UNION
(select nazir_id, nazir_value from
(select fawad_id, fawad_value, nazir_id, nazir_value from fawad right outer join nazir on fawad_id = nazir_id)
where fawad_id is NULL)
UNION
(select nazir_id, nazir_value from
(select fawad_id, fawad_value, nazir_id, nazir_value from fawad full outer join nazir on fawad_id = nazir_id)
where fawad_id is not NULL and nazir_id is not NULL)
)
order by fawad_id;
Method: 2 ( UNION ALL & NOT Exists)
select * from
(
select nazir_id, nazir_value from nazir
UNION
select fawad_id, fawad_value from fawad where not exists (select 1 from nazir where fawad.fawad_id = nazir.nazir_id)
)
order by nazir_id;
Method: 3 (Using COALESCE)
select COALESCE(nazir_id, fawad_id) as ID, COALESCE(nazir_value, fawad_value) as VALUE from fawad full outer join nazir on fawad.fawad_id = nazir.nazir_id order by ID;
No comments:
Post a Comment