Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1077923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:38:53+00:00 2026-05-16T21:38:53+00:00

I have an audit table which stores audit rows for a long running process

  • 0

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

  1. Parent1, 09:30:00, 09:40:00, Valid
  2. Child1, 09:30:01, 09:35:00, Valid
  3. Child2, 09:35:01, 09:40:00, Valid
  4. Parent2, 09:40:01, null, null
  5. 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:

  1. Not be any overlapping audit records otherwise will double count the duration.
  2. Need to handle the fact that there may be tasks which are still running for the current date and return a null
  3. 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.
  4. 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

  1. Parent1, 09:30:00, 09:40:01, Valid
  2. Parent2, 09:40:01, null, null

Any ideas?

Thanks

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:38:53+00:00Added an answer on May 16, 2026 at 9:38 pm

    Found quite a few “interesting” things about EF and dates while figuring out how to do this.

    So in the end I:

    1. Extended the audit log table to add in a new nullable DateTime StartDateTimeOfNext and private _startDateTimeOfNext to cache the previous value. Once calculated I didn’t want to have to calculate it again
    2. Added an Entity Context to the same partial class
    3. Extended the Audit table class to add the code below.
    4. I used DateTime.Today for the DefaultIfEmpty as SQL Server 2005 doesn’t support the C# DateTime.MinValue and DateTime.MaxValue
    5. Its not particularly beautiful but it works
      public DateTime? StartDateTimeOfNext
    {  
        get  
        {  
            if (_startDateTimeOfNext == null)  
            {  
                DateTime windowMin = this.StartTime.Date;
                DateTime windowMax = this.StartTime.Date.AddDays(1).AddMinutes(-1);
                DateTime? query = (from nextAuditLog in AuditLog.AuditLogEntityContext.AuditLogs
                where nextAuditLog.auditId > this.auditId 
                && nextAuditLog.StartTime > this.StartTime 
                && nextAuditLog.StartTime <= windowMax 
                && nextAuditLog.StartTime >= windowMin
                orderby nextAuditLog.auditId ascending
                select nextAuditLog.StartTime
                ).DefaultIfEmpty(DateTime.Today).First();               
                if (query != DateTime.Today)
                {
                    _startDateTimeOfNext = query;
                    return query;
                }
                else
                {
                    _startDateTimeOfNext = this.EndTime;
                    return this.EndTime;
                }
            }
            else
            {
            return _startDateTimeOfNext;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an audit table in my SQL Server 2008 database which contains the
I have a table called 'Audit' in SQL Server 2005 like this: Name |
I have inherited a SQL Server 2008 database to which calling applications have access
I use SQL Server 2005 and have the below question: On a table A
I have created a trigger on a table which holds an audit log. When
We are using audit tables for each operational table, which stores the previous value
For example I have a table which stores details about properties. Which could have
I have an audit table setup which essentially mirrors one of my tables along
Background I have a production SQL Server 2005 server to which 4 different applications
We have a audit table which tracks the changes in master table by an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.