USE JOB
GO
 
 
DECLARE @Jobs TABLE(JobKey_Start INT, JobKey_End INT, Jobname NVARCHAR(50), INDEX Idx_JobKey_Start NONCLUSTERED (JobKey_Start), INDEX Idx_JobKey_End NONCLUSTERED (JobKey_End))
 
 
INSERT INTO @Jobs (JobKey_Start, JobKey_End, Jobname)
	SELECT JOB_KEY AS JobKey_Start, dbo.fn_GetJobEnd(JOB_KEY) AS JobKey_End, JOBNAME
	FROM dbo.JOB_STATE
	WHERE [STATE] = 'RUNNING'
		AND ENTRYDATE BETWEEN '2025-12-01' AND '2026-03-01'
		AND (JOBNAME LIKE 'IndexProdukte[_]%' OR 
		JOBNAME LIKE 'IndexViewer[_]%' OR 
		JOBNAME LIKE 'Product[_]%')
--SELECT DISTINCT Jobname FROM @Jobs
 
SELECT
	CONVERT(NVARCHAR(16), StartDt, 104) AS StartDt,
	--DATENAME(WEEKDAY, StartDt) AS [Day],
			SUM(res.IndexProdukte__Generate__XML_Data) AS IndexProdukte__Generate__XML_Data,
			SUM(res.IndexProdukte__Import__SourceData) AS IndexProdukte__Import__SourceData,
			SUM(res.IndexProdukte__Publish__for_QS) AS IndexProdukte__Publish__for_QS,
			SUM(res.IndexProdukte__QS_Data_Integrity_Checks) AS IndexProdukte__QS_Data_Integrity_Checks,
			SUM(res.IndexProdukte__Update__Stamm_Daten) AS IndexProdukte__Update__Stamm_Daten,
			SUM(res.IndexProdukte__Update__Work_Stamm_Daten) AS IndexProdukte__Update__Work_Stamm_Daten,
			SUM(res.IndexViewer__DL__insureINDEX) AS IndexViewer__DL__insureINDEX,
			SUM(res.IndexViewer__DL_UL__Daily) AS IndexViewer__DL_UL__Daily,
			SUM(res.IndexViewer__UL__insureINDEX) AS IndexViewer__UL__insureINDEX,
			SUM(res.Product__Generate_Superset) AS Product__Generate_Superset
FROM (
	SELECT CAST(StartDt AS DATE) AS StartDt, 
			IndexProdukte__Generate__XML_Data,
			IndexProdukte__Import__SourceData,
			IndexProdukte__Publish__for_QS,
			IndexProdukte__QS_Data_Integrity_Checks,
			IndexProdukte__Update__Stamm_Daten,
			IndexProdukte__Update__Work_Stamm_Daten,
			IndexViewer__DL__insureINDEX,
			IndexViewer__DL_UL__Daily,
			IndexViewer__UL__insureINDEX,
			Product__Generate_Superset
	FROM (
		SELECT jss.JOBNAME, jss.ENTRYDATE AS [StartDt], jse.ENTRYDATE AS [EndDt], (DATEDIFF(MINUTE, jss.ENTRYDATE, jse.ENTRYDATE)) AS Duration
		FROM @Jobs AS js
		INNER JOIN dbo.JOB_STATE AS jss ON js.JobKey_Start = jss.JOB_KEY
		INNER JOIN dbo.JOB_STATE AS jse ON js.JobKey_End = jse.JOB_KEY
	) AS t
	PIVOT (
		SUM(Duration)
		FOR JOBNAME IN (
			IndexProdukte__Generate__XML_Data,
			IndexProdukte__Import__SourceData,
			IndexProdukte__Publish__for_QS,
			IndexProdukte__QS_Data_Integrity_Checks,
			IndexProdukte__Update__Stamm_Daten,
			IndexProdukte__Update__Work_Stamm_Daten,
			IndexViewer__DL__insureINDEX,
			IndexViewer__DL_UL__Daily,
			IndexViewer__UL__insureINDEX,
			Product__Generate_Superset
			)
	) AS pivot_table
) AS res
GROUP BY res.StartDt
ORDER BY res.StartDt ASC