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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:33:18+00:00 2026-06-15T06:33:18+00:00

I have a report that I need to create from a UserHistory table in

  • 0

I have a report that I need to create from a UserHistory table in SQL Server 2008 R2. A sample is below:
report

For billing purposes we need to calculate how many distinct users have had a particular status across a particular period. The period can be as low as 1 day or up to something like 2 months. So if one user was active, not active and then active again across a period then that counts as one active user.

The curve ball now comes in to play. If I had no histories within the specified period, the stored proc needs to hunt for the first history entry before the start of the period to see if it matches the status. As obviously if I was active on the 20th of October and then my account was suspended on the 15th of November, if the period was for November, I was still active.

At first I did this with a cursor going through each User table, and doing the queries per user and it’s user histories however even with the relevant indexes this takes about 40 seconds for 200000 users. 40s for each day in a period for 9 statuses adds up very quickly.

My colleage and I came up with this method which has come the closest:

ALTER PROCEDURE [dbo].[cpUserHistory_CountByStatus] @StartDate DATETIME2,
    @EndDate DATETIME2,
    @Status VARCHAR(50)
AS
SET NOCOUNT ON

SELECT SUM(CASE WHEN CurrentMonth.UserId IS NOT NULL OR PreviousTime.UserId IS NOT NULL THEN 1 ELSE 0 END)
FROM [User]
LEFT JOIN
       (SELECT UserId
       FROM
              UserHistory
              INNER JOIN
                     (SELECT MAX(UserHistoryId) AS UserHistoryId
                     FROM UserHistory
                     WHERE EditedDate BETWEEN @StartDate AND @EndDate
                     GROUP BY UserId) LastStatus ON UserHistory.UserHistoryId = LastStatus.UserHistoryId
       WHERE UserHistory.Status = @Status                                -- EDIT: Your status you're interested in
       ) CurrentMonth ON [User].UserId = CurrentMonth.UserId
LEFT JOIN
       (SELECT UserId
       FROM
              UserHistory
              INNER JOIN
                     (SELECT MAX(UserHistoryId) AS UserHistoryId
                     FROM UserHistory
                     WHERE EditedDate < @StartDate
                     GROUP BY UserId) LastStatus ON UserHistory.UserHistoryId = LastStatus.UserHistoryId
       WHERE UserHistory.Status = @Status                               -- EDIT: Your status you're interested in
       ) PreviousTime ON [User].UserId = PreviousTime.UserId

But as you can see in the simplified report sample, a user was cancelled on the 3rd of the month and then reactivated on the 5th, however the total does not reflect that at some point in the period there was 1 cancelled user.

Does anyone have any ideas or better ways to go about doing this? I could really use some input.

  • 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-06-15T06:33:23+00:00Added an answer on June 15, 2026 at 6:33 am

    I settled on calling a stored procedure once for the entire range which will bring back all UserHistory entries which matches my criteria and in code I can count and filter it as I please.

    Not ideal as there could be quite a lot of data transfered from the DB to the application, and once in the app it can use a bit of memory – but it works, is fast and more importantly is accurate.

    ALTER PROCEDURE [dbo].[cpUserHistory_SelectForBillingPeriod] @StartDate DATETIME2,
        @EndDate DATETIME2
    AS
    BEGIN
        DECLARE @Uh TABLE (
            UserHistoryId INT,
            UserId INT,
            UserStatus VARCHAR(50),
            EditedDate DATETIME2
            )
    
        INSERT INTO @Uh
        SELECT UserHistoryId,
            UserId,
            [Status],
            EditedDate
        FROM UserHistory
        WHERE EditedDate >= @StartDate
            AND EditedDate <= @EndDate
    
        INSERT INTO @Uh
        SELECT UserHistory.UserHistoryId,
            UserId,
            [Status],
            EditedDate
        FROM UserHistory
        INNER JOIN (
            SELECT MAX(UserHistoryId) AS UserHistoryId
            FROM UserHistory
            WHERE EditedDate < @StartDate
            GROUP BY UserId
            ) LastStatus ON UserHistory.UserHistoryId = LastStatus.UserHistoryId
    
        SELECT *
        FROM @Uh
        order by EditedDate desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a report that I created in SQL Server Reporting Services 2008. I
I have a stock table and I would like to create a report that
I have a need to create a report in DynamicJasper that has multiple subreports
I have to create a report that will compare values from different years. The
I have a complicated report that I need to draw with GDI+ (I don't
I have a report that is getting info from bsis, bsas, bsid, bsad, vbrk,
I have a report that I run from a system once a week, and
I have this Birt report that I inherited from another developer, consisting of a
I have a organization name table with the following structure given below: CREATE TABLE
I need to create a report that a user can toggle through data going

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.