refactor(psi): rebuilt the delivery and planning data to 2.0 data

This commit is contained in:
2026-06-16 18:52:58 -05:00
parent 2ebf695526
commit 6a14bab30c
4 changed files with 133 additions and 173 deletions

View File

@@ -1,32 +1,72 @@
use AlplaPROD_test1
declare @start_date nvarchar(30) = '[startDate]' --'2025-01-01'
declare @end_date nvarchar(30) = '[endDate]' --'2025-08-09'
/*
articles will need to be passed over as well as the date structure we want to see
*/
use [test1_AlplaPROD2.0_Read]
select x.IdArtikelvarianten As Article,
ProduktionAlias as Description,
standort as MachineId,
MaschinenBezeichnung as MachineName,
--MaschZyklus as PlanningCycleTime,
x.IdProdPlanung as LotNumber,
FORMAT(ProdTag, 'MM/dd/yyyy') as ProductionDay,
x.planMenge as TotalPlanned,
ProduktionMenge as QTYPerDay,
round(ProduktionMengeVPK, 2) PalDay,
Status as finished
--MaschStdAuslastung as nee
from dbo.V_ProdLosProduktionJeProdTag_PLANNING (nolock) as x
left join
dbo.V_ProdPlanung (nolock) as p on
x.IdProdPlanung = p.IdProdPlanung
where ProdTag between @start_date and @end_date
and p.IdArtikelvarianten in ([articles])
--and V_ProdLosProduktionJeProdTag_PLANNING.IdKunde = 10
--and IdProdPlanung = 18442
order by ProdTag desc
DECLARE @start_date date = '[startDate]'; --'2025-01-01'
DECLARE @end_date date = '[endDate]'; --'2025-08-09'
DECLARE @tz sysname = 'Eastern Standard Time'; -- usday1; use 'Central Standard Time' for usksc1
DECLARE @shiftSeconds int = 7*3600; -- 07:00 production-day anchor
--TODO: add in the proper time zone based on the env, get correcr shift time as well
;WITH src AS (
SELECT
pl.RunningNumber, pl.ArticleHumanReadableId, pl.ArticleAlias, pl.ArticleDescription,
pl.MachineLocation, pl.MachineDescription, pl.ProductionLotState, pl.ProductionInterrupt,
pl.PlanQuantityPieces, pl.PlanQuantityLoadingUnit,
CAST(pl.ProdStart AT TIME ZONE @tz AS datetime2(0)) AS StartLocal,
CAST(pl.PlanEnd AT TIME ZONE @tz AS datetime2(0)) AS EndLocal
FROM productionScheduling.ProductionLot AS pl
WHERE pl.PlanEnd > pl.ProdStart
and pl.publishState = 1
and pl.ArticleHumanReadableId IN ([articles]) -- <-- article filter (was IdArtikelvarianten)
--AND pl.RunningNumber = 28094 -- <-- lot filter (was IdProdPlanung); comment out for all
),
calc AS (
SELECT *,
DATEADD(SECOND, @shiftSeconds,
CAST(CASE WHEN DATEDIFF(SECOND, CAST(StartLocal AS date), StartLocal) >= @shiftSeconds
THEN CAST(StartLocal AS date)
ELSE DATEADD(DAY,-1, CAST(StartLocal AS date)) END AS datetime2(0))) AS FirstBoundary
FROM src
),
days AS ( -- one row per production day the lot touches
SELECT RunningNumber, ArticleHumanReadableId, ArticleAlias, ArticleDescription, MachineLocation,
MachineDescription, ProductionLotState, PlanQuantityPieces, PlanQuantityLoadingUnit,
StartLocal, EndLocal, FirstBoundary AS DayStart
FROM calc
UNION ALL
SELECT RunningNumber, ArticleHumanReadableId, ArticleAlias, ArticleDescription, MachineLocation,
MachineDescription, ProductionLotState, PlanQuantityPieces, PlanQuantityLoadingUnit,
StartLocal, EndLocal, DATEADD(DAY,1,DayStart)
FROM days
WHERE DATEADD(DAY,1,DayStart) < EndLocal
),
seg AS ( -- working seconds inside each production day
SELECT *,
DATEDIFF(SECOND,
CASE WHEN StartLocal > DayStart THEN StartLocal ELSE DayStart END,
CASE WHEN EndLocal < DATEADD(DAY,1,DayStart) THEN EndLocal ELSE DATEADD(DAY,1,DayStart) END
) AS SegSec
FROM days
),
cum AS ( -- cumulative seconds for telescoping round
SELECT *,
SUM(SegSec) OVER (PARTITION BY RunningNumber ORDER BY DayStart ROWS UNBOUNDED PRECEDING) AS CumEnd,
SUM(SegSec) OVER (PARTITION BY RunningNumber ORDER BY DayStart ROWS UNBOUNDED PRECEDING) - SegSec AS CumStart,
SUM(SegSec) OVER (PARTITION BY RunningNumber) AS DenomSec
FROM seg
)
SELECT
ArticleHumanReadableId AS Article,
ArticleAlias AS Description,
MachineLocation AS MachineId,
MachineDescription AS MachineName,
RunningNumber AS LotNumber,
FORMAT(DayStart, 'MM/dd/yyyy') AS ProductionDay,
PlanQuantityPieces AS TotalPlanned,
ROUND(PlanQuantityPieces * 1.0 * CumEnd / DenomSec, 0)
- ROUND(PlanQuantityPieces * 1.0 * CumStart / DenomSec, 0) AS QTYPerDay,
ROUND(PlanQuantityLoadingUnit * CumEnd / DenomSec, 2)
- ROUND(PlanQuantityLoadingUnit * CumStart / DenomSec, 2) AS PalDay,
ProductionLotState AS finished
FROM cum
WHERE CAST(DayStart AS date) BETWEEN @start_date AND @end_date -- filter AFTER cumulative calc
ORDER BY RunningNumber, DayStart DESC
OPTION (MAXRECURSION 366);