Here we go:
SELECT * FROM (
select
ROW_NUMBER() OVER (ORDER BY thedate ASC) AS rownumber,
sum(delay) as delay, label, thedate as start_date, DATEADD(hour, {?StepPara},thedate) AS stop_date from (
SELECT (
SELECT DiffDateA + DiffDateB + DiffDateC + DiffDateD
FROM
(
-- A case
select
case
(
select COUNT(*)
from dly_delay
--FORMAT (GETDATE (), 'yyyy/M/dd HH:00:00', 'en-US')
where start_delay < FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay > FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
)
when 0 then 0
else
(
select DATEDIFF(MI,FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') ,end_delay) AS DiffDateA --start date
from dly_delay
where start_delay < FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay > FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
)
end AS DiffDateA
) A
,
(
-- B case
select
case
(
select COUNT(*)
from dly_delay
where start_delay > FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay < FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
when 0 then 0
else
(
select SUM(DATEDIFF(MI,start_delay, end_delay)) AS DiffDateB
from dly_delay
where start_delay > FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay < FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
end AS DiffDateB
) B
,
(
-- C case
select
case
(
select COUNT(*)
from dly_delay
where start_delay < FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
and end_delay > FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
when 0 then 0
else
(
select DATEDIFF(MI,start_delay, FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US')) AS DiffDateC --end date
from dly_delay
where start_delay < FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
and end_delay > FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
end AS DiffDateC
) C
,
(
-- D case
select
case
(
select COUNT(*)
from dly_delay
where start_delay < FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay > FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
when 0 then 0
else
(
select DATEDIFF(MI,FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') , FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US')) AS DiffDateD
from dly_delay
where start_delay < FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay > FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
end AS DiffDateD
) D
) AS delay
, (DATEPART(HOUR, START_DELAY)) AS label
, START_DELAY AS thedate
FROM (
SELECT thedate AS start_delay
, DATEADD(hour, {?StepPara}, thedate) AS end_delay
FROM dbo.ExplodeDates2({?StopPara},DATEADD(hour, -{?StepPara}, {?StartPara}))
) mainDelay
WHERE START_DELAY>={?StopPara} AND END_DELAY<={?StartPara}
GROUP BY DATEPART(MONTH, START_DELAY)
, DATEPART(DAY, START_DELAY)
, (DATEPART(HOUR, START_DELAY) / {?StepPara})
, START_DELAY
, END_DELAY
UNION
-- add the gaps
(
SELECT 0 AS delay
, (DATEPART(HOUR, thedate)) AS label
, thedate AS thedate
FROM dbo.ExplodeDates2({?StopPara},{?StartPara})
EXCEPT
SELECT (
SELECT DiffDateA + DiffDateB + DiffDateC + DiffDateD
FROM
(
-- A case
select
case
(
select COUNT(*)
from dly_delay
--FORMAT (GETDATE (), 'yyyy/M/dd HH:00:00', 'en-US')
where start_delay < FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay > FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
)
when 0 then 0
else
(
select DATEDIFF(MI,FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') ,end_delay) AS DiffDateA --start date
from dly_delay
where start_delay < FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay > FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
)
end AS DiffDateA
) A
,
(
-- B case
select
case
(
select COUNT(*)
from dly_delay
where start_delay > FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay < FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
when 0 then 0
else
(
select SUM(DATEDIFF(MI,start_delay, end_delay)) AS DiffDateB
from dly_delay
where start_delay > FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay < FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
end AS DiffDateB
) B
,
(
-- C case
select
case
(
select COUNT(*)
from dly_delay
where start_delay < FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
and end_delay > FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
when 0 then 0
else
(
select DATEDIFF(MI,start_delay, FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US')) AS DiffDateC --end date
from dly_delay
where start_delay < FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
and end_delay > FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
end AS DiffDateC
) C
,
(
-- D case
select
case
(
select COUNT(*)
from dly_delay
where start_delay < FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay > FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
when 0 then 0
else
(
select DATEDIFF(MI,FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') , FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US')) AS DiffDateD
from dly_delay
where start_delay < FORMAT(mainDelay.START_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --start date
and end_delay > FORMAT(mainDelay.END_DELAY, 'yyyy/M/dd HH:00:00', 'en-US') --end date
)
end AS DiffDateD
) D
) AS delay
, (DATEPART(HOUR, START_DELAY)) AS label
, START_DELAY AS thedate
FROM (
SELECT thedate AS start_delay
, DATEADD(hour, {?StepPara}, thedate) AS end_delay
FROM dbo.ExplodeDates2({?StopPara},DATEADD(hour, -{?StepPara}, {?StartPara}))
) mainDelay
WHERE START_DELAY>={?StopPara} AND END_DELAY<={?StartPara}
GROUP BY DATEPART(MONTH, START_DELAY)
, DATEPART(DAY, START_DELAY)
, (DATEPART(HOUR, START_DELAY) / {?StepPara})
, START_DELAY
, END_DELAY
)
) a
GROUP BY label, thedate
) AS foo
WHERE rownumber % {?StepPara} = 0