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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:14:09+00:00 2026-06-13T06:14:09+00:00

I want to write a procedure in TSQL that calculates peak times between given

  • 0

I want to write a procedure in TSQL that calculates peak times between given date ranges that splitted to time given.

Start time: 10-02-2012 10:00
End time  : 10-02 2012 11:00
time range: every 5 minutes

so it will be:

10:00 range 1  -> 5 peak times
10:05 range 2  -> 11 peak times
.
.
.
11:00 range 11 -> 7 peak times

when time range given 30 min then the code will calculate 2 ranges

Should I use Interval? How can i solve this problem? Any Help?

  • 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-13T06:14:10+00:00Added an answer on June 13, 2026 at 6:14 am

    You can determine the time ranges thusly:

    declare @StartTime as DateTime = '10-02-2012 10:00'
    declare @EndTime as DateTime ='10-02-2012 11:00'
    declare @TimeRange as Time = '00:05:00.000'
    
    ; with TimeRanges as (
      select @StartTime as StartTime, @StartTime + @TimeRange as EndTime
      union all
      select StartTime + @TimeRange, EndTime + @TimeRange
        from TimeRanges
        where EndTime < @EndTime ) -- Corrected.
      select StartTime, EndTime
        from TimeRanges
    

    Join the ranges with your sample data to obtain the summary:

    declare @StartTime as DateTime = '10-02-2012 10:00'
    declare @EndTime as DateTime ='10-02-2012 11:00'
    declare @TimeRange as Time = '00:05:00.000'
    
    declare @Samples as Table ( SampleId Int Identity, SampleTime DateTime )
    insert into @Samples ( SampleTime ) values
      ( '10-02-2012 9:00' ), ( '10-02-2012 10:00' ), ( '10-02-2012 10:02' ), ( '10-02-2012 10:02' ),
      ( '10-02-2012 10:05' ), ( '10-02-2012 10:20' ), ( '10-02-2012 10:34' ), ( '10-02-2012 11:30' )
    
    ; with TimeRanges as (
      select @StartTime as StartTime, @StartTime + @TimeRange as EndTime
      union all
      select StartTime + @TimeRange, EndTime + @TimeRange
        from TimeRanges
        where EndTime < @EndTime ) -- Corrected.
      select StartTime, EndTime, Count( S.SampleId ) as Samples
        from TimeRanges as TR left outer join
          @Samples as S on TR.StartTime <= S.SampleTime and S.SampleTime < TR.EndTime
        group by TR.StartTime, TR.EndTime
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a stored procedure that will accept a parameter of @searchString.
I want to write stored procedure in visual studio that as a parameter recieves
I want to write a stored procedure that works something like this: SELECT *
I want to write a PLSQL stored procedure that accepts a table name as
I want to write a procedure (function) that checks if a string contains another
I want to write a stored procedure that accept an XML parameter, parsing it's
I want to write a stored procedure. In that stored procedure, I want to
I want to write a stored procedure that queries XML files after I have
I want to write a stored procedure that lists the name of all the
I want to write a stored procedure for my Tables that have a similar

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.