SSRS Calculate & Display the Duration Between Records Given Date & Time Fields

Dear Experts,

Please see the attached report which contains a number of alarm notification records. The right most field is a status field where the value '3' is in-alarm state and the value '5' is out-of-alarm state. I would like to replace this column with a new column called 'Down Time' and show the total down time for each RTU. In that case my new report would also have no 'Date' or 'Time' columns but start with LAN - RTU etc. ending with the new 'Down Time' column. The point of the report is to show the total down time of each RTU equipment over selected dates. The date selector is already configured using Report Parameters @StarDate and @EndDate. The example report contains different RTU records where both the alarm and no-alarm states have been recorded for different RTUs.

The SQL query used to extract the data from the data source is as follows. Note that the [Categories] field is used by the Application to pass over multiple attribute values separated by a ';'. These are extracted using the SPLIT function and then used to form the columns of the report e.g. LAN.Value - RTU.Value - CDC.Value - RTU Location.Value etc.

    [Name], [UTC_Reception_DateTime], [Categories], [Label], [Priority], [Enum_Status], [Cycle]
        [Name], [UTC_Reception_DateTime], [Categories], [Label], [Priority], [Enum_Status], [Cycle]
        ,ROW_NUMBER() OVER (PARTITION BY [Name] ORDER BY Cycle DESC) AS row_num
        FROM History) AS subquery1
WHERE [UTC_Reception_DateTime] BETWEEN @StartDate AND @EndDate
ORDER BY [UTC_Reception_DateTime] DESC

Thanks, Gary
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nico BontenbalCommented:
Does this sample help:
--create temp table with sample data
create table #history(Slot varchar(10), Reception_DateTime  datetime, Enum_Status int)

insert into #history values 
('1RK-UA0679','20150526 9:52:01',3),
('1RK-UA0679','20150526 9:52:02',5),
('1RK-UA0572','20150526 9:54:37',3),
('1RK-UA0572','20150526 9:54:55',5),
('1RK-UA0543','20150526 9:55:01',3),
('1RK-UA0543','20150526 9:55:17',5)

--Sample query to show the downtime
    h.Reception_DateTime StartDownTime,
    (select  min(Reception_DateTime) from #history h2 where  h2.Slot = h.Slot and h2.Reception_DateTime >= h.Reception_DateTime and h2.Enum_Status = 5) as EndDownTime
    #history h
    Enum_Status = 3

--drop the temp tabel
drop table #history

Open in new window

When you have your data like this you can group by Slot on the report and do something like sum(datediff(second,StartDownTime,EndDownTime)) in the report. Or you could turn the query into a subquery and do the grouping and totaling on SQL server.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.