Unpivot avoid cartesian product on empty columns
-
13-01-2021 - |
Question
I have an issue with UNPIVOT.
The initial situation is that I receive some files with a fixed number of columns and I have to import them in my Database like that. That means my table has for example 10 departement columns : departement_1, department_2, departement_3, ...
Based on that data, I need to create some views where I only have 1 column because I am limited by the software where I'll be importing the data.
This is a view I made:
SELECT reference, libelle, date_naissance, pays_naissance, nationalite,
pays_fiscal, nif, us_tin, us_person, type_activite, etat_civil, rac,
pep, departement, etat_individu, relation
FROM S2i_individu
LEFT JOIN relation_individu
ON S2i_individu.reference = relation_individu.individu
UNPIVOT (
pays_fiscal FOR t1 IN (pays_fiscal_1, pays_fiscal_2, pays_fiscal_3)
) UNPIV
UNPIVOT (
nif FOR t2 IN (nif_1, nif_2, nif_3)
) UNPIV
UNPIVOT (
departement for t3 in (departement_1, departement_2, departement_3, departement_4, departement_5, departement_6, departement_7, departement_8, departement_9, departement_10)
) UNPIV
WHERE import_id = (SELECT TOP(1) id FROM import ORDER BY import_date DESC)
My issue is that by the nature of UNPIVOT, the result is the cartesian product of departement, nif and pays_fiscal. Which means that I will always have 90 rows for each entry in S2i_individu.
Does anyone know a way to limit to 1 row if there's no data ? Like if I don't have any data for pays_fiscal, nif and departement, that I still have a single row in my result?
Solution
This is what I came up with the help of reddit :
SELECT DISTINCT reference, libelle, date_naissance, pays_naissance, nationalite, pays_fiscal, nif, us_tin, us_person, type_activite, etat_civil, rac, pep, departement, etat_individu, relation
FROM S2i_individu
LEFT JOIN relation_individu ON S2i_individu.reference = relation_individu.individu
CROSS APPLY (
VALUES
(pays_fiscal_1,nif_1,departement_1,'t1'),
(pays_fiscal_2,nif_2,departement_2,'t2'),
(pays_fiscal_3,nif_3,departement_3,'t3'),
('','',departement_4,'t4'),
('','',departement_5,'t5'),
('','',departement_6,'t6'),
('','',departement_7,'t7'),
('','',departement_8,'t8'),
('','',departement_9,'t9'),
('','',departement_10,'t10')
) AS v(pays_fiscal, nif, departement, suffix)
WHERE import_id = (SELECT TOP(1) id FROM import ORDER BY import_date DESC