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
| CREATE PROC [dbo].[RecupererCentres]
(@PeriodeDeb varchar(7),@PeriodeFin varchar(7), @Ratio varchar(50))
AS
DECLARE @Tmp_Sortie table (Periode varchar(7), Cen_B1 varchar(50), Cen_B2 varchar(50), Cen_B3 varchar(50), Cen_H1 varchar(50), Cen_H2 varchar(50), Cen_H3 varchar(50), NbCen int)
DECLARE @Tmp_Bas table (NbRank smallint, Centre varchar(10), Per varchar(7))
DECLARE @Tmp_Haut table (NbRank smallint, Centre varchar(10), Per varchar(7))
DECLARE @Tmp_NbCen table (Per varchar(7), Tmp_NbCen int)
SET NOCOUNT ON
BEGIN
INSERT INTO @Tmp_Sortie(Periode)
SELECT DISTINCT RATIO.RAT_Periode AS Periode
FROM RATIO
WHERE (RATIO.RAT_Nom IN (@Ratio)) AND (RATIO.RAT_Periode BETWEEN @PeriodeDeb AND @PeriodeFin)
INSERT INTO @Tmp_Bas
SELECT Rank_Bas, TAB_CEN_BAS.CEN_Code_FK AS CEN_Bas, RAT_Periode FROM
( SELECT RATIO.CEN_Code_FK AS CEN_Code_FK, RANK() OVER(PARTITION BY RAT_Periode ORDER BY RATIO.RAT_Valeur ASC) AS Rank_Bas, RAT_Periode
FROM RATIO
WHERE (RAT_Periode BETWEEN @PeriodeDeb AND @PeriodeFin) AND (RATIO.RAT_Nom IN (@Ratio))
) AS TAB_CEN_BAS
WHERE Rank_Bas <=3
UPDATE @Tmp_Sortie SET Cen_B1 = Centre FROM @Tmp_Sortie AS Tmp_Sortie INNER JOIN @Tmp_Bas AS Tmp_Bas ON Tmp_Sortie.Periode = Tmp_Bas.Per AND Tmp_Bas.NbRank = 1
UPDATE @Tmp_Sortie SET Cen_B2 = Centre FROM @Tmp_Sortie AS Tmp_Sortie INNER JOIN @Tmp_Bas AS Tmp_Bas ON Tmp_Sortie.Periode = Tmp_Bas.Per AND Tmp_Bas.NbRank = 2
UPDATE @Tmp_Sortie SET Cen_B3 = Centre FROM @Tmp_Sortie AS Tmp_Sortie INNER JOIN @Tmp_Bas AS Tmp_Bas ON Tmp_Sortie.Periode = Tmp_Bas.Per AND Tmp_Bas.NbRank = 3
INSERT INTO @Tmp_Haut
SELECT Rank_Haut, TAB_CEN_HAUT.CEN_Code_FK AS CEN_Haut, RAT_Periode FROM
( SELECT RATIO.CEN_Code_FK AS CEN_Code_FK, RANK() OVER(PARTITION BY RAT_Periode ORDER BY RATIO.RAT_Valeur DESC) AS Rank_Haut, RAT_Periode
FROM RATIO
WHERE (RAT_Periode BETWEEN @PeriodeDeb AND @PeriodeFin) AND (RATIO.RAT_Nom IN (@Ratio))
) AS TAB_CEN_HAUT
WHERE Rank_Haut <=3
UPDATE @Tmp_Sortie SET Cen_H1 = CENTRE FROM @Tmp_Sortie AS Tmp_Sortie INNER JOIN @Tmp_Haut AS Tmp_Haut ON Tmp_Sortie.Periode = Tmp_Haut.Per AND Tmp_Haut.NbRank = 1
UPDATE @Tmp_Sortie SET Cen_H2 = CENTRE FROM @Tmp_Sortie AS Tmp_Sortie INNER JOIN @Tmp_Haut AS Tmp_Haut ON Tmp_Sortie.Periode = Tmp_Haut.Per AND Tmp_Haut.NbRank = 2
UPDATE @Tmp_Sortie SET Cen_H3 = CENTRE FROM @Tmp_Sortie AS Tmp_Sortie INNER JOIN @Tmp_Haut AS Tmp_Haut ON Tmp_Sortie.Periode = Tmp_Haut.Per AND Tmp_Haut.NbRank = 3
INSERT INTO @Tmp_NbCen
SELECT DISTINCT RATIO.RAT_Periode AS Periode, COUNT(RATIO.CEN_Code_FK) AS Tmp_NbCen
FROM RATIO
WHERE (RATIO.RAT_Nom IN (@Ratio))
GROUP BY RATIO.RAT_Periode
UPDATE @Tmp_Sortie SET NbCen = Tmp_NbCen FROM @Tmp_Sortie AS Tmp_Sortie INNER JOIN @Tmp_NbCen AS Tmp_NbCen ON Tmp_Sortie.Periode = Tmp_NbCen.Per
END |
Partager