I have an audit table which stores audit rows for a long running process at an individual task level in SQL Server.
- auditId int,
- TaskName nvarchar(255),
- StartTime datetime,
- EndTime (datetime, null),
- Status (nvarchar(255), null)
The process contains parent and child steps and each step is logged to the audit table. A task may or may not have a parent.
e.g.
auditId, TaskName, StartTime, EndTime, Status
- Parent1, 09:30:00, 09:40:00, Valid
- Child1, 09:30:01, 09:35:00, Valid
- Child2, 09:35:01, 09:40:00, Valid
- Parent2, 09:40:01, null, null
- Child3, 09:40:02, null, null
Using C# 4.0 / LINQ to Entities / LINQ over an ObservableCollection I would like to get an Audit summary from this table for the past X days containing the total duration for each day by status. I can pivot the individual records to generate the summary if I can figure out how to get the summary audit records.
In the Audit summary there should:
- Not be any overlapping audit records otherwise will double count the duration.
- Need to handle the fact that there may be tasks which are still running for the current date and return a null
- Need to handle the lag between the end of the previous task and the start of the next task. Probably by getting the duration between the start of the current task and the start of the next task instead of the end of the current task.
- Handle the null for the final task of the day which won't have a following start time if I use the start time of the next record as described in point 3
So using the example above the summary records would be:
auditId, TaskName, StartTime, EndTime, Status
- Parent1, 09:30:00, 09:40:01, Valid
- Parent2, 09:40:01, null, null
Any ideas?
Thanks