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
| ; With CONVOYEUR_SEL (no_convoyeur, no_point_decision_from, no_point_decision_to, longueur, vitesse, correctif, rn, etape) as
(
select cvy.no_convoyeur, cvy.no_point_decision_from, cvy.no_point_decision_to, cvy.longueur, cvy.vitesse, cvy.correctif
, row_number() over(partition by no_point_decision_from, no_point_decision_to order by etp.no_etape desc, longueur desc)
, case when etp.no_etape is not null then 1 else 0 end
from dbo.CONVOYEUR as cvy
left outer join dbo.ETAPES as etp
on etp.NO_CONVOYEUR = cvy.NO_CONVOYEUR
)
, CONVOYEUR_MIN (no_convoyeur, no_point_decision_from, no_point_decision_to, longueur, vitesse, correctif, etape) as
(
select no_convoyeur, no_point_decision_from, no_point_decision_to, longueur, vitesse, correctif, etape
from CONVOYEUR_SEL
where rn = 1
)
, CTEst (no_point_decision_from, no_point_decision_to, longueur, temps_parcours, chemin, fin_convoyage, nb_etapes_obl) as
(
select null, pde.no_point_decision, 0, cast(0.0 AS float)
, '"' + cast(pde.no_point_decision as varchar(max)) + '"'
, pde.fin_convoyage, 0
from dbo.POINT_DECISION as pde
where pde.debut_convoyage = 1
union all
select pdf.no_point_decision
, pdt.no_point_decision
, cte.longueur + cvy.longueur
, cte.temps_parcours + case cvy.vitesse when 0 then 0.0 else (cvy.longueur / (1000.0*cvy.vitesse)) + (cvy.correctif/60.0) end
, cte.chemin + ',"' + cast(pdt.no_point_decision as varchar) + '"'
, pdt.fin_convoyage
, cte.nb_etapes_obl + cvy.etape
from CTest as cte
inner join CONVOYEUR_MIN as cvy
on cvy.no_point_decision_from = cte.no_point_decision_to
inner join dbo.POINT_DECISION as pdf
on pdf.no_point_decision = cvy.no_point_decision_from
inner join dbo.POINT_DECISION as pdt
on pdt.no_point_decision = cvy.no_point_decision_to
where charindex('"' + cast(cvy.no_point_decision_to as varchar) + '"', cte.chemin) = 0
)
, Choix (chemin, longueur, temps_parcours, rn) as
(
select replace(chemin, '"', '') as chemin
, longueur
, temps_parcours
, row_number() over(order by longueur asc)
from CTest
where fin_convoyage = 1
and nb_etapes_obl = (select count(*) from dbo.ETAPES)
)
select chemin, longueur, temps_parcours
from Choix
where rn = 1
chemin longueur temps_parcours
----------------------------------------------------------- -------- ----------------
36,24,20,13,1,6,7,8,9,10,21,14,15,2,3,4,5,22,12,17,27,18,33 461537 188,571447402597 |
Partager