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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| WITH
pred(rchch,Etiquette,[ETA_Id], [ETA_DateControle],[ETA_Predecesseur_ETA_Id],[ETA_Successeur_ETA_Id], [ETA_DateDebut],[ETA_DateFin], new_surface,
JOUR, CHANGE, niveau, CASTING)
AS (SELECT 'pred', T_Etablissement.Etiquette , T_Etablissement.[ETA_Id], [ETA_DateControle],[ETA_Predecesseur_ETA_Id],[ETA_Successeur_ETA_Id], [ETA_DateDebut],[ETA_DateFin], [VINFODTBX].new_surface,
0, CAST([ETA_ApeEtablissement_APE_Id] as varchar(max)) , 0,
CAST(T_Etablissement.ETA_Id as varchar(max))
FROM [Observatoire].dbo.T_Etablissement
INNER JOIN [Observatoire].dbo.T_Entreprise
ON T_Entreprise.ENT_Id = T_Etablissement.ETA_Entreprise_ENT_Id
INNER JOIN [Observatoire].[EXPLOITATION].[VINFODTBX]
ON [VINFODTBX].ETA_Id=T_Etablissement.ETA_Id
WHERE [VINFODTBX].new_surface is not null
UNION ALL
SELECT 'pred', P.Etiquette, P.ETA_Id ,P.[ETA_DateControle],P.[ETA_Predecesseur_ETA_Id],P.[ETA_Successeur_ETA_Id], P.[ETA_DateDebut],P.[ETA_DateFin],[VINFODTBX].new_surface,
/*JOUR*/ DATEDIFF(DAY,t.ETA_DateDebut,P.ETA_DateFin),
/*CHANGE*/ CASE WHEN t.CHANGE!=CAST(P.ETA_ApeEtablissement_APE_Id as varchar(max)) then CAST('1' as varchar(max)) else CAST('0' as varchar(max)) end,
/*NIVEAU*/ t.niveau + 1,
/*CASTING*/ CASTING + ', '+CAST(P.ETA_Id as varchar(max))
FROM [Observatoire].[dbo].[T_Etablissement] P
INNER JOIN [Observatoire].dbo.T_Entreprise
ON T_Entreprise.ENT_Id = P.ETA_Entreprise_ENT_Id
INNER JOIN [Observatoire].[EXPLOITATION].[VINFODTBX]
ON [VINFODTBX].ETA_Id=P.ETA_Id
INNER JOIN pred t on P.ETA_Id=t.ETA_Predecesseur_ETA_Id
WHERE CASTING not like '%, ' + cast(P.ETA_Id as varchar(40)) + '%'
)
,
succ(rchch, Etiquette,[ETA_Id],[ETA_DateControle],[ETA_Predecesseur_ETA_Id],[ETA_Successeur_ETA_Id], [ETA_DateDebut],[ETA_DateFin], new_surface,
JOUR, CHANGE, niveau, CASTING)
AS (SELECT 'succ', T_Etablissement.Etiquette, T_Etablissement.[ETA_Id],[ETA_DateControle],[ETA_Predecesseur_ETA_Id],[ETA_Successeur_ETA_Id],[ETA_DateDebut], [ETA_DateFin],[VINFODTBX].new_surface,
0, CAST(ETA_ApeEtablissement_APE_Id as varchar(max)), 0,
CAST(T_Etablissement.ETA_Id as varchar(max))
FROM [Observatoire].dbo.T_Etablissement
INNER JOIN [Observatoire].dbo.T_Entreprise
ON T_Entreprise.ENT_Id = T_Etablissement.ETA_Entreprise_ENT_Id
INNER JOIN [Observatoire].[EXPLOITATION].[VINFODTBX]
ON [VINFODTBX].ETA_Id=T_Etablissement.ETA_Id
WHERE [VINFODTBX].new_surface is not null
UNION ALL
SELECT 'succ', S.Etiquette, S.[ETA_Id],S.[ETA_DateControle],S.[ETA_Predecesseur_ETA_Id],S.[ETA_Successeur_ETA_Id], S.[ETA_DateDebut], S.[ETA_DateFin],[VINFODTBX].new_surface,
/*JOUR*/ DATEDIFF(DAY,tt.ETA_DateDebut,S.ETA_DateFin),
/*CHANGE*/ CASE WHEN tt.CHANGE!=CAST(S.ETA_ApeEtablissement_APE_Id as varchar(max)) then CAST('1' as varchar(max)) else CAST('0' as varchar(max)) end,
/*NIVEAU*/ tt.niveau + 1,
/*CASTING*/ CASTING + ', '+CAST(S.ETA_Id as varchar(max))
FROM [Observatoire].[dbo].[T_Etablissement] S
INNER JOIN [Observatoire].dbo.T_Entreprise
ON T_Entreprise.ENT_Id = S.ETA_Entreprise_ENT_Id
INNER JOIN [Observatoire].[EXPLOITATION].[VINFODTBX]
ON [VINFODTBX].ETA_Id=S.ETA_Id
INNER JOIN succ tt on S.ETA_Id=tt.ETA_Successeur_ETA_Id
WHERE CASTING not like '%, ' + cast(S.ETA_Id as varchar(40)) + '%'
and not exists(select ETA_Id from pred where pred.ETA_Id=S.ETA_Id)
)
select DISTINCT niveau , SPACE(niveau)+Etiquette as Etiquette
,[ETA_Id],[ETA_DateControle],[ETA_Predecesseur_ETA_Id],[ETA_Successeur_ETA_Id], [ETA_DateDebut],
[ETA_DateFin],new_surface,JOUR,CHANGE,CASTING
from (Select * from pred
union
Select * from succ --where not exists(select ETA_Id from pred where pred.ETA_Id=succ.ETA_Id)
) as info
order by CASTING |
Partager