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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:01:26+00:00 2026-06-09T08:01:26+00:00

I have a table that contains the following information: Conf_Start_Time Part_Start_Time Part_End_Time A record

  • 0

I have a table that contains the following information:

Conf_Start_Time
Part_Start_Time
Part_End_Time

A record is considered to be active at time t if t falls between Part_Start_Time and Part_End_Time.

What I’d like to do is analyze all the records to determine how many records are active given a specified day. My proposed solution is to loop through each minute in the day (say from 6AM to 9PM) and check each record on that day to determine if the user was active at the specified time, t.

Is there a solution to this in SQL, or should I proceed with the code solution?

In code I would pull all the records to memory, loop through time (6AM to 9PM) and test each record on the specified day to determine if it was active at the current time. If it’s active, I would increment a counter, if not, proceed to the next record. Next time around, reinitialize the counter and proceed looping through the day.

We’re using SQL Server 2005.

UPDATE: The output I’m looking for would be an array of maximum concurrent usage from 6AM to 9PM

Record  Conf_Start_Time    Part_Start_Time     Part_End_Time
1.      6/5/2012 13:40:00  6/5/2012 13:41:23   6/5/2012 13:45:27
2.      6/5/2012 13:40:00  6/5/2012 13:40:23   6/5/2012 13:47:29
3.      6/5/2012 13:40:00  6/5/2012 13:42:55   6/5/2012 13:44:17

So at time 13:40:00 0 records are active; at time 13:41:00 1 record is active; at time 13:42:00 2 records are active; at time 13:43:00 3 records are active;

And I need the data for each minute in the day. And then each day in the month. Can this type of looping even be done in SQL?

  • 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-09T08:01:27+00:00Added an answer on June 9, 2026 at 8:01 am

    The following uses correlated subqueries to get the numbers you want. The idea is to count the number of cumulative starts and cumulative ends, up to each time:

    with alltimes as
        (select t.*
         from ((select part_start_time as thetime, 1 as IsStart, 0 as IsEnd
                from t
               ) union all
               (select part_end_time, 0 as isStart, 1 as IsEnd
                from t
               )
              ) t
         )
    select t.*,
           (cumstarts - cumends) as numactive
    from (select alltimes.thetime,
                 (select sum(isStart)
                  from allStarts as where as.part_start_time <= alltimes.thetime
                 ) as cumStarts,
                 (select sum(isEnd)
                  from allStarts as where as.part_end_time <= alltimes.thetime
                 ) as cumEnds
          from alltimes
         ) t
    

    The output is based on each time present in the data.

    As a rule of thumb, you don’t want to be doing lots of data work on the application side. When possible, that is best done in the database.

    This query will have duplicates when there are multiple starts and ends at the same time. In this case, you would need to determine how to treat this case. But, the idea is the same. The outer select would be:

    select t.thetime, max(cumstarts - cumends) as numactives
    

    and you need a group by clause:

    group by t.thetime
    

    The “max” gives the starts precedence (meaning with the same time stampt, the starts are treated as happening first, so you get the maximum actives at that time). “Min” would give the ends precedence. And, if you use average, remember to convert to floating point:

    select t.thetime, avg(cumstarts*1.0 - cumends) as avgnumactives
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains the following information date sales 2011-02-13 1 2011-02-13
I have a table that contains the following information date sales 2011-02-13 1 2011-02-13
I have a table that contains the following columns: ID int, DISTANCE float, EVENT
I have the following requirement. I have a table with a column that contains
I have a table that contains a record of prioritys that links to another
I have a table that contains the following columns; a, b, e and another
I have a table with that contains information that I would like to show
I have the following database design question: I have a record that contains the
I have a huge table in my database that contains distances between cities. This
I have a table that contains multiple dropdown menus for a list of profile

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.