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
| With Demande (idDemande, memNom) as
(
select 1, 'Luc' union all
select 2, 'Manon'
)
, Document (idDocument, docCode) as
(
select 1, 'Doc1' union all
select 2, 'Doc2' union all
select 3, 'Doc3'
)
, DemandeDocument (idDemande, idDocument, ddEnvoyer) as
(
select 1, 1, cast(1 as bit) union all
select 2, 1, cast(0 as bit) union all
select 2, 3, cast(1 as bit)
)
, SR (memNom, docCode, ddEnvoyer) as
(
select dmd.memNom, doc.docCode, cast(ddo.ddEnvoyer as int)
from Demande as dmd
cross join Document as doc
left outer join DemandeDocument as ddo
on ddo.idDemande = dmd.idDemande
and ddo.idDocument = doc.idDocument
)
select memNom, [Doc1], [Doc2], [Doc3]
from SR
pivot (max(ddEnvoyer) for docCode in ([Doc1], [Doc2], [Doc3])) as pvt;
memNom Doc1 Doc2 Doc3
------ ----------- ----------- -----------
Luc 1 NULL NULL
Manon 0 NULL 1 |
Partager