我有两个复杂的查询,我需要将它们的两个结果集都放在一个表中。问题是,一个拥有cte,另一个没有。我不太了解这些查询,因为它是别人写的。
union
convert cte to subquery
SELECT DISTINCT
CodeFoyer,
CodeOuvrage,
LibDomaine,
LIB_OUVRAGECAT,
--POSE_OUVRAGE,
ACCES_OUVRAGE,
--DIVERS_OUVRAGE,
--OBSERVATION_OUVRAGE,
LIB_REGIME,
VOIEENT_VOIE,
--NUMVOIE_OUVRAGE,
NATURE_VOIE,
LIB_VOIE,
LIB_VOIEDOM,
LIB_SECTEUR,
LIB_REGION_TECHNIQUE,
LIB_REGION_ADMIN,
COD_LUMINAIRE,
LIB_LUMINAIRE,
LIB_FOURNISSEUR_LUMINAIRE,
XLUMELEC,
COD_LAMPE,
LIB_LAMPE,
LIB_LAMPEFAM,
Ordre,
DateInstallationLuminaire
FROM(
SELECT DISTINCT
E2.ELECLE AS CodeFoyer
,E.ELECLE AS CodeOuvrage
,d.DOMLIBELLE AS LibDomaine
,et.ETYLIBELLE AS LIB_OUVRAGECAT
,CAT.CTXTEXTE AS ACCES_OUVRAGE
,NRJ_R.NRELIBELLE AS LIB_REGIME
,VE.VENLIBELLE AS VOIEENT_VOIE
,V.VOINATURE AS NATURE_VOIE
,V.VOILIBELLE AS LIB_VOIE
,EC.ELCNUMVOIE AS LIB_VOIEDOM
, S.SECLIBELLE AS LIB_SECTEUR
, reg.REGLIBELLE AS LIB_REGION_TECHNIQUE
, PR.PROLIBELLE AS LIB_REGION_ADMIN
,MB.MBICLE AS COD_LUMINAIRE
,MB.MBILIBELLE AS LIB_LUMINAIRE
,F.FOUCLE AS LIB_FOURNISSEUR_LUMINAIRE
,CASE
WHEN MAT.MTXBO OLEEN = 1
THEN -1
ELSE 0
END AS XLUMELEC
,MB2.MBICLE AS COD_LAMPE
,MB.MBILIBELLE AS LIB_LAMPE
,MTF.MTFLIBELLE AS LIB_LAMPEFAM
,NRJC.NCELIBELLE AS CentreFacturation
,NRJ_R.NRECLE AS CodeRegime
,NRJT.NTACLE AS CodeTarif
,NRJT.NTALIBELLE AS Tarif
,ECO.ECOORDRE AS Ordre
,eco.ECODATEINST AS DateInstallationLuminaire
,ROW_NUMBER() OVER(PARTITION BY VG2.IDPERE ORDER BY VG2.enfantOrdre ASC ) AS nb
FROM
ELEMENT E
INNER JOIN ELEMENTCONTENEUR EC ON EC.ID_ELEMENT = E.ID_ELEMENT
INNER JOIN ELEMENTCOMPO ECO ON ECO.ID_ELEMENT = E.ID_ELEMENT
INNER JOIN MATBIBLIO MB ON MB.ID_MATBIBLIO = ECO.ID_MATBIBLIO
INNER JOIN MATERIELTYPE MT ON MT.ID_MATERIELTYPE = MB.ID_MATERIELTYPE --AND (MT.ID_MATERIELTYPE = 146)
INNER JOIN VGRAPHECOMPO VG ON VG.IDPERE = ECO.ID_ELEMENTCOMPO
INNER JOIN VGRAPHECOMPO VG2 ON VG2.IDENFANT = VG.IDENFANT --AND VG2.ENFANTTYPE = 148
INNER JOIN ELEMENTCOMPO ECO2 ON ECO2.ID_ELEMENTCOMPO = VG2.IDENFANT
INNER JOIN MATBIBLIO MB2 ON MB2.ID_MATBIBLIO = ECO2.ID_MATBIBLIO
--LEFT JOIN VGRAPHECOMPO VG3 ON VG3.IDENFANT = VG.IDPERE AND VG3.PERETYPE = 145
--LEFT JOIN ELEMENTCOMPO ECO3 ON ECO3.ID_ELEMENTCOMPO = VG3.IDPERE
--LEFT JOIN MATBIBLIO MB3 ON MB3.ID_MATBIBLIO = ECO3.ID_MATBIBLIO
INNER JOIN ELEMENTMATGERE EMA ON EMA.ID_ELEMENTCOMPO = ECO2.ID_ELEMENTCOMPO
INNER JOIN ELEMENT E2 ON E2.ID_ELEMENT = EMA.ID_ELEMENT
left JOIN NRJELEMENTCOMPO NRJ_ECO ON NRJ_ECO.ID_ELEMENTCOMPO = ECO2.ID_ELEMENTCOMPO
left JOIN NRJREGIME NRJ_R ON NRJ_R.ID_NRJREGIME = NRJ_ECO.ID_NRJREGIME
left JOIN NRJTARIF NRJT ON NRJT.ID_NRJTARIF = NRJ_ECO.ID_NRJTARIF
left JOIN NRJCENTREFACTURATION NRJC ON NRJC.ID_NRJCENTREFACTURATION = NRJ_ECO.ID_NRJCENTREFACTURATION
LEFT JOIN MATATTRIBUTSUPP MAT ON MAT.ID_ELEMENTCOMPO = VG.IDPERE --AND mat.ID_MATATTRIBUTSUPPMETA = 555
INNER JOIN DOMAINE D ON E.ID_DOMAINE =D.ID_DOMAINE AND d.ID_DOMAINE=3
INNER JOIN ELEMENTTYPE ET ON EC.ID_ELEMENTTYPE =ET.ID_ELEMENTTYPE
LEFT JOIN CATATTRIBUTSUPP CAT ON E.ID_ELEMENT =CAT.ID_ELEMENT
INNER JOIN VOIE V ON EC.ID_VOIE =V.ID_VOIE
INNER JOIN VOIEENTIERE VE ON V.ID_VOIEENTIERE =VE.ID_VOIEENTIERE
INNER JOIN SECTEUR S ON V.ID_SECTEUR =S.ID_SECTEUR
INNER JOIN REGION REG ON S.ID_REGION =REG.ID_REGION
LEFT JOIN FOURNISSEUR F ON E.ID_FOURNISSEUR =F.ID_FOURNISSEUR
INNER JOIN MATFAMILLE MTF ON MB.ID_MATFAMILLE = MTF.ID_MATFAMILLE
INNER JOIN PROPRIETAIRE PR ON EC.ID_PROPRIETAIRE =PR.ID_PROPRIETAIRE
) AS D
WHERE NB = 1
with cte
as
(
select
p.ID_MATERIELTYPE,
p.ID_MATBIBLIO,
CodeLampe,
LibelleLampe,
CodeFamilleLampe,
LibelleFamilleLampe
from
(select distinct
j.ID_MATERIELTYPE,
j.ID_MATBIBLIO,
j.MBICLE as CodeLampe
,j.MBILIBELLE as LibelleLampe
,j.ID_MATFAMILLE as CodeFamilleLampe
,mf.MTFLIBELLE as LibelleFamilleLampe
from MATBIBLIO mbs
inner join (
select
mb.ID_MATBIBLIO ,
mb.ID_MATERIELTYPE,
mb.ID_MATFAMILLE,
mb.MBICLE,
mb.MBILIBELLE
from MATBIBLIO mb
where ID_MATERIELTYPE in ( select
ID_MATERIELTYPE
from
MATERIELTYPE mt
where
ID_DOMAINE = 1 and mt.MTYLIBELLE like '%lampe%'
)
) j on mbs.ID_MATBIBLIO = j.ID_MATBIBLIO
inner join MATBIBLIOSUPPMETA supmeta on mbs.ID_MATERIELTYPE = supmeta.ID_MATERIELTYPE
left join MATBIBLIOSUPPMETAVALEUR v on v.ID_MATBIBLIOSUPPMETA = supmeta.ID_MATBIBLIOSUPPMETA
inner join MATFAMILLE mf on mf.ID_MATFAMILLE = j.ID_MATFAMILLE
group by
j.ID_MATBIBLIO ,
j.ID_MATERIELTYPE,
j.ID_MATFAMILLE,
j.MBILIBELLE,
mf.MTFLIBELLE,
j.MBICLE,
supmeta.MABNOM,
supmeta.ID_MATBIBLIOSUPPMETA
) as p
)
select
CodeLampe,
LibelleLampe,
CodeFamilleLampe,
LibelleFamilleLampe,
coalesce(mbsPN.[MBXTEXTE],mbsPN.[MBXENTIER],mbsPN.[MBXREEL]) as PuissanceNominaleWatt,
coalesce(mbsPCFW.[MBXTEXTE],mbsPCFW.[MBXENTIER],mbsPCFW.[MBXREEL]) as PuissanceConsommeeFerroWatt,
coalesce(mbsPCEW.[MBXTEXTE],mbsPCEW.[MBXENTIER],mbsPCEW.[MBXREEL]) as PuissanceConsommeeElecWatt,
coalesce(mbsF.[MBXTEXTE],mbsF.[MBXENTIER],mbsF.[MBXREEL]) as FluxLampe,
v.MBVLIBELLE as ClasseLampe
from cte
left join [MATBIBLIOSUPP] mbsPN on mbsPN.ID_MATBIBLIO = cte.ID_MATBIBLIO and mbsPN.ID_MATBIBLIOSUPPMETA = 1323
left join [MATBIBLIOSUPP] mbsPCFW on mbsPCFW.ID_MATBIBLIO = cte.ID_MATBIBLIO and mbsPCFW.ID_MATBIBLIOSUPPMETA = 1383
left join [MATBIBLIOSUPP] mbsPCEW on mbsPCEW.ID_MATBIBLIO = cte.ID_MATBIBLIO and mbsPCEW.ID_MATBIBLIOSUPPMETA = 1398
left join [MATBIBLIOSUPP] mbsF on mbsF.ID_MATBIBLIO = cte.ID_MATBIBLIO and mbsF.ID_MATBIBLIOSUPPMETA = 1324
left join MATBIBLIOSUPPMETAVALEUR v on v.ID_MATBIBLIOSUPPMETAVALEUR =
(select MBXENTIER from MATBIBLIOSUPP where ID_MATBIBLIOSUPPMETA = 1322 and ID_MATBIBLIO = cte.ID_MATBIBLIO)
答案 0 :(得分:0)
我认为查询是颠倒的。下面是经过重新设计的查询,带有一些注释。
with cte as (
select p.ID_MATERIELTYPE
, p.ID_MATBIBLIO
, CodeLampe
, LibelleLampe
, CodeFamilleLampe
, LibelleFamilleLampe
FROM (
SELECT DISTINCT j.ID_MATERIELTYPE
, j.ID_MATBIBLIO
, j.MBICLE as CodeLampe
, j.MBILIBELLE as LibelleLampe
, j.ID_MATFAMILLE as CodeFamilleLampe
, mf.MTFLIBELLE as LibelleFamilleLampe
FROM MATBIBLIO mbs
INNER JOIN (
select mb.ID_MATBIBLIO ,
mb.ID_MATERIELTYPE,
mb.ID_MATFAMILLE,
mb.MBICLE,
mb.MBILIBELLE
from MATBIBLIO mb
/* Whhere the ID is in the subquery. The subquery should return more than 1 result, but 1 is okay, too. */
where ID_MATERIELTYPE in (
select ID_MATERIELTYPE
from MATERIELTYPE mt
where ID_DOMAINE = 1
/* PATINDEX() will help to find records better than like */
/* If a value over 0 is returned, then the value to match was found. */
and PATINDEX('%lampe%', mt.MTYLIBELLE) > '0'
)
) AS j on mbs.ID_MATBIBLIO = j.ID_MATBIBLIO
inner join MATBIBLIOSUPPMETA AS supmeta on mbs.ID_MATERIELTYPE = supmeta.ID_MATERIELTYPE
left join MATBIBLIOSUPPMETAVALEUR AS v on v.ID_MATBIBLIOSUPPMETA = supmeta.ID_MATBIBLIOSUPPMETA
inner join MATFAMILLE AS mf ON mf.ID_MATFAMILLE = j.ID_MATFAMILLE
--group by j.ID_MATBIBLIO ,
--j.ID_MATERIELTYPE,
--j.ID_MATFAMILLE,
--j.MBILIBELLE,
--mf.MTFLIBELLE,
--j.MBICLE,
--supmeta.MABNOM,
--supmeta.ID_MATBIBLIOSUPPMETA
) as p
)
select CodeLampe
, LibelleLampe
, CodeFamilleLampe
, LibelleFamilleLampe
/* COALESCE(a, b) <- if a is NULL, then b will be used. If a and b are NULL, then NULL is returned. */
, COALESCE(mbsPN.[MBXTEXTE],mbsPN.[MBXENTIER],mbsPN.[MBXREEL]) as PuissanceNominaleWatt
, COALESCE(mbsPCFW.[MBXTEXTE],mbsPCFW.[MBXENTIER],mbsPCFW.[MBXREEL]) as PuissanceConsommeeFerroWatt
, COALESCE(mbsPCEW.[MBXTEXTE],mbsPCEW.[MBXENTIER],mbsPCEW.[MBXREEL]) as PuissanceConsommeeElecWatt
, COALESCE(mbsF.[MBXTEXTE],mbsF.[MBXENTIER],mbsF.[MBXREEL]) as FluxLampe
, v.MBVLIBELLE as ClasseLampe
, NULL AS CodeFoyer
, NULL AS CodeOuvrage
, NULL AS LibDomaine
, NULL AS LIB_OUVRAGECAT
--, NULL AS POSE_OUVRAGE
, NULL AS ACCES_OUVRAGE
--, NULL AS DIVERS_OUVRAGE
--, NULL AS OBSERVATION_OUVRAGE
, NULL AS LIB_REGIME
, NULL AS VOIEENT_VOIE
--, NULL AS NUMVOIE_OUVRAGE
, NULL AS NATURE_VOIE
, NULL AS LIB_VOIE
, NULL AS LIB_VOIEDOM
, NULL AS LIB_SECTEUR
, NULL AS LIB_REGION_TECHNIQUE
, NULL AS LIB_REGION_ADMIN
, NULL AS COD_LUMINAIRE
, NULL AS LIB_LUMINAIRE
, NULL AS LIB_FOURNISSEUR_LUMINAIRE
, NULL AS XLUMELEC
, NULL AS COD_LAMPE
, NULL AS LIB_LAMPE
, NULL AS LIB_LAMPEFAM
, NULL AS Ordre
, NULL AS DateInstallationLuminaire
from cte
left join [MATBIBLIOSUPP] mbsPN on mbsPN.ID_MATBIBLIO = cte.ID_MATBIBLIO and mbsPN.ID_MATBIBLIOSUPPMETA = 1323
left join [MATBIBLIOSUPP] mbsPCFW on mbsPCFW.ID_MATBIBLIO = cte.ID_MATBIBLIO and mbsPCFW.ID_MATBIBLIOSUPPMETA = 1383
left join [MATBIBLIOSUPP] mbsPCEW on mbsPCEW.ID_MATBIBLIO = cte.ID_MATBIBLIO and mbsPCEW.ID_MATBIBLIOSUPPMETA = 1398
left join [MATBIBLIOSUPP] mbsF on mbsF.ID_MATBIBLIO = cte.ID_MATBIBLIO and mbsF.ID_MATBIBLIOSUPPMETA = 1324
left join MATBIBLIOSUPPMETAVALEUR v on v.ID_MATBIBLIOSUPPMETAVALEUR = (
select MBXENTIER
from MATBIBLIOSUPP
where ID_MATBIBLIOSUPPMETA = 1322
and ID_MATBIBLIO = cte.ID_MATBIBLIO)
-- Union results from CTE with the query below
UNION
SELECT DISTINCT
NULL AS CodeLampe
, NULL AS LibelleLampe
, NULL AS CodeFamilleLampe
, NULL AS LibelleFamilleLampe
, NULL AS PuissanceNominaleWatt
, NULL AS PuissanceConsommeeFerroWatt
, NULL AS PuissanceConsommeeElecWatt
, NULL AS FluxLampe
, NULL AS ClasseLampe
, CodeFoyer
, CodeOuvrage
, LibDomaine
, LIB_OUVRAGECAT
--, POSE_OUVRAGE
, ACCES_OUVRAGE
--, DIVERS_OUVRAGE
--, OBSERVATION_OUVRAGE
, LIB_REGIME
, VOIEENT_VOIE
--, NUMVOIE_OUVRAGE
, NATURE_VOIE
, LIB_VOIE
, LIB_VOIEDOM
, LIB_SECTEUR
, LIB_REGION_TECHNIQUE
, LIB_REGION_ADMIN
, COD_LUMINAIRE
, LIB_LUMINAIRE
, LIB_FOURNISSEUR_LUMINAIRE
, XLUMELEC
, COD_LAMPE
, LIB_LAMPE
, LIB_LAMPEFAM
, Ordre
, DateInstallationLuminaire
FROM (
SELECT DISTINCT E2.ELECLE AS CodeFoyer
, E.ELECLE AS CodeOuvrage
, d.DOMLIBELLE AS LibDomaine
, et.ETYLIBELLE AS LIB_OUVRAGECAT
, CAT.CTXTEXTE AS ACCES_OUVRAGE
, NRJ_R.NRELIBELLE AS LIB_REGIME
, VE.VENLIBELLE AS VOIEENT_VOIE
, V.VOINATURE AS NATURE_VOIE
, V.VOILIBELLE AS LIB_VOIE
, EC.ELCNUMVOIE AS LIB_VOIEDOM
, S.SECLIBELLE AS LIB_SECTEUR
, reg.REGLIBELLE AS LIB_REGION_TECHNIQUE
, PR.PROLIBELLE AS LIB_REGION_ADMIN
, MB.MBICLE AS COD_LUMINAIRE
, MB.MBILIBELLE AS LIB_LUMINAIRE
, F.FOUCLE AS LIB_FOURNISSEUR_LUMINAIRE
,CASE WHEN MAT.MTXBOOLEEN = '1' THEN '-1' ELSE '0' END AS [XLUMELEC]
,MB2.MBICLE AS COD_LAMPE
,MB.MBILIBELLE AS LIB_LAMPE
,MTF.MTFLIBELLE AS LIB_LAMPEFAM
,NRJC.NCELIBELLE AS CentreFacturation
,NRJ_R.NRECLE AS CodeRegime
,NRJT.NTACLE AS CodeTarif
,NRJT.NTALIBELLE AS Tarif
,ECO.ECOORDRE AS Ordre
,eco.ECODATEINST AS DateInstallationLuminaire
,ROW_NUMBER() OVER(PARTITION BY VG2.IDPERE ORDER BY VG2.enfantOrdre ASC) AS nb
FROM ELEMENT AS E
INNER JOIN ELEMENTCONTENEUR AS EC ON EC.ID_ELEMENT = E.ID_ELEMENT
INNER JOIN ELEMENTCOMPO AS ECO ON ECO.ID_ELEMENT = E.ID_ELEMENT
INNER JOIN MATBIBLIO AS MB ON MB.ID_MATBIBLIO = ECO.ID_MATBIBLIO
INNER JOIN MATERIELTYPE AS MT ON MT.ID_MATERIELTYPE = MB.ID_MATERIELTYPE --AND (MT.ID_MATERIELTYPE = 146)
INNER JOIN VGRAPHECOMPO AS VG ON VG.IDPERE = ECO.ID_ELEMENTCOMPO
INNER JOIN VGRAPHECOMPO AS VG2 ON VG2.IDENFANT = VG.IDENFANT --AND VG2.ENFANTTYPE = 148
INNER JOIN ELEMENTCOMPO AS ECO2 ON ECO2.ID_ELEMENTCOMPO = VG2.IDENFANT
INNER JOIN MATBIBLIO AS MB2 ON MB2.ID_MATBIBLIO = ECO2.ID_MATBIBLIO
--LEFT JOIN VGRAPHECOMPO AS VG3 ON VG3.IDENFANT = VG.IDPERE AND VG3.PERETYPE = 145
--LEFT JOIN ELEMENTCOMPO AS ECO3 ON ECO3.ID_ELEMENTCOMPO = VG3.IDPERE
--LEFT JOIN MATBIBLIO AS MB3 ON MB3.ID_MATBIBLIO = ECO3.ID_MATBIBLIO
INNER JOIN ELEMENTMATGERE AS EMA ON EMA.ID_ELEMENTCOMPO = ECO2.ID_ELEMENTCOMPO
INNER JOIN ELEMENT AS E2 ON E2.ID_ELEMENT = EMA.ID_ELEMENT
left JOIN NRJELEMENTCOMPO AS NRJ_ECO ON NRJ_ECO.ID_ELEMENTCOMPO = ECO2.ID_ELEMENTCOMPO
left JOIN NRJREGIME AS NRJ_R ON NRJ_R.ID_NRJREGIME = NRJ_ECO.ID_NRJREGIME
left JOIN NRJTARIF AS NRJT ON NRJT.ID_NRJTARIF = NRJ_ECO.ID_NRJTARIF
left JOIN NRJCENTREFACTURATION AS NRJC ON NRJC.ID_NRJCENTREFACTURATION = NRJ_ECO.ID_NRJCENTREFACTURATION
LEFT JOIN MATATTRIBUTSUPP AS MAT ON MAT.ID_ELEMENTCOMPO = VG.IDPERE --AND mat.ID_MATATTRIBUTSUPPMETA = 555
INNER JOIN DOMAINE AS D ON E.ID_DOMAINE =D.ID_DOMAINE AND d.ID_DOMAINE=3
INNER JOIN ELEMENTTYPE AS ET ON EC.ID_ELEMENTTYPE =ET.ID_ELEMENTTYPE
LEFT JOIN CATATTRIBUTSUPP AS CAT ON E.ID_ELEMENT =CAT.ID_ELEMENT
INNER JOIN VOIE AS V ON EC.ID_VOIE =V.ID_VOIE
INNER JOIN VOIEENTIERE AS VE ON V.ID_VOIEENTIERE =VE.ID_VOIEENTIERE
INNER JOIN SECTEUR AS S ON V.ID_SECTEUR = S.ID_SECTEUR
INNER JOIN REGION AS REG ON S.ID_REGION = REG.ID_REGION
LEFT JOIN FOURNISSEUR AS F ON E.ID_FOURNISSEUR = F.ID_FOURNISSEUR
INNER JOIN MATFAMILLE AS MTF ON MB.ID_MATFAMILLE = MTF.ID_MATFAMILLE
INNER JOIN PROPRIETAIRE AS PR ON EC.ID_PROPRIETAIRE =PR.ID_PROPRIETAIRE
) AS D
WHERE NB = 1
--Resulting columns
/*
CodeLampe,
LibelleLampe,
CodeFamilleLampe,
LibelleFamilleLampe,
PuissanceNominaleWatt,
PuissanceConsommeeFerroWatt,
PuissanceConsommeeElecWatt,
FluxLampe,
ClasseLampe,
CodeFoyer,
CodeOuvrage,
LibDomaine,
LIB_OUVRAGECAT,
ACCES_OUVRAGE,
LIB_REGIME,
VOIEENT_VOIE,
NATURE_VOIE,
LIB_VOIE,
LIB_VOIEDOM,
LIB_SECTEUR,
LIB_REGION_TECHNIQUE,
LIB_REGION_ADMIN,
COD_LUMINAIRE,
LIB_LUMINAIRE,
LIB_FOURNISSEUR_LUMINAIRE,
XLUMELEC,
COD_LAMPE,
LIB_LAMPE,
LIB_LAMPEFAM,
Ordre,
DateInstallationLuminaire
*/