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 882225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:23:43+00:00 2026-05-15T12:23:43+00:00

I have a table with an audit log: BugId Timestamp Status 1 2010-06-24 10:00:00

  • 0

I have a table with an audit log:

BugId       Timestamp               Status
1           2010-06-24 10:00:00     open
2           2010-06-24 11:00:00     open
1           2010-06-25 12:00:00     closed
2           2010-06-26 13:00:00     closed

I want a running total of open and closed bugs like:

Timestamp            #       Status
2010-06-25 00:00:00  2       open
2010-06-26 00:00:00  1       open
2010-06-26 00:00:00  1       closed
2010-06-27 00:00:00  2       closed

How may I do this query (or similar) in Microsoft SQL Server 2000?

The output is intended to be used to feed a time series chart so I do not care if there are rows with 0 output since I will probably only select a timespan like the last month.

  • 1 1 Answer
  • 0 Views
  • 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-15T12:23:44+00:00Added an answer on May 15, 2026 at 12:23 pm

    I think the output actually matches the sample data: on the 25th (12am), there are two open bugs. On the 26th, there is one open bug and one closed. And by the 27th, all bugs are closed.

    It isn’t clear how the main dates should be created. For my example, I pre-loaded the dates that I knew to be right but this could be accomplished in a variety of ways depending on the requirements of the user.

    Anyway, the code is below. This should work for instances where a bug is opened and closed multiple times on the same day. It operates under the assumption that a bug cannot be opened and closed at the same time.

    /** Setup the tables **/
    IF OBJECT_ID('tempdb..#bugs') IS NOT NULL DROP TABLE #bugs
    
    CREATE TABLE #bugs (
      BugID INT,
      [Timestamp] DATETIME,
      [Status] VARCHAR(10)
    ) 
    
    IF OBJECT_ID('tempdb..#dates') IS NOT NULL DROP TABLE #dates
    
    CREATE TABLE #dates ( 
      [Date] DATETIME
    ) 
    
    /** Load the sample data. **/
    INSERT #bugs 
    SELECT 1, '2010-06-24 10:00:00', 'open'   UNION ALL
    SELECT 2, '2010-06-24 11:00:00', 'open'   UNION ALL
    SELECT 1, '2010-06-25 12:00:00', 'closed' UNION ALL
    SELECT 2, '2010-06-26 13:00:00', 'closed' 
    
    /** Build an arbitrary date table **/
    INSERT #dates 
    SELECT '2010-06-24' UNION ALL  
    SELECT '2010-06-25' UNION ALL  
    SELECT '2010-06-26' UNION ALL  
    SELECT '2010-06-27' 
    
    
    /** 
    Subquery x:
    For each date in the #date table,
    get the BugID and it's last status.
    This is for BugIDs that have been
    opened and closed on the same day.
    
    Subquery y:
    Drawing from subquery x, get the 
    date, BugID, and Status of its
    last status for that day
    
    Main query:
    For each date, get the count
    of the most recent statuses for 
    that date. This will give the
    running totals of open and 
    closed bugs for each date
    **/
    SELECT
      [Date],
      COUNT(*) AS [#],
      [Status]
    FROM (
      SELECT 
        Date,
        x.BugID,
        b.[Status]
      FROM ( 
        SELECT
          [Date],
          BugID,
          MAX([Timestamp]) AS LastStatus
        FROM #dates d 
        INNER JOIN #bugs b 
        ON d.[Date] > b.[Timestamp]
        GROUP BY
          [Date],
          BugID
        ) x 
      INNER JOIN #bugs b
      ON x.BugID = b.BugID
      AND x.LastStatus = b.[Timestamp]
    ) y 
    GROUP BY [Date], [Status]
    ORDER BY [Date], CASE WHEN [Status] = 'Open' THEN 1 ELSE 2 END
    

    Results:

    Date                    #           Status
    ----------------------- ----------- ----------
    2010-06-25 00:00:00.000 2           open
    2010-06-26 00:00:00.000 1           open
    2010-06-26 00:00:00.000 1           closed
    2010-06-27 00:00:00.000 2           closed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called 'Audit' in SQL Server 2005 like this: Name |
We have a simple table (an audit log) that our (3rd-party) product fills with
My memory is failing me. I have a simple audit log table based on
I have created a trigger on a table which holds an audit log. When
I have an audit table which stores audit rows for a long running process
I have a quick question. I'm have a db an audit Table with a
I have an audit record table that I am writing to. I am connecting
I am using LINQ to SQL with single table inheritance for an audit log.
Say I have some data stored in an audit table, where triggers on the
I log insert and update info to every table create_date TIMESTAMP create_user_id INT update_date

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.