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

  • Home
  • SEARCH
  • 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 832977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:26:46+00:00 2026-05-15T04:26:46+00:00

Just something interesting come in my mind. Assume that we have a table (in

  • 0

Just something interesting come in my mind. Assume that we have a table (in SQL Server) like this:

  • Location
  • Velocity
  • Time

for example:

Location     Velocity   Time
1            40         1:20
2            35         2:00
3            45         2:05
4            50         2:30
5            60         2:45
6            48         2:55
7            40         3:00
8            35         3:15
9            50         3:20
10           70         3:30
11           50         3:35
12           40         3:40

Assume that speed barrier is 40kph, the output is something like this

Starttime         Endtime
2:05              3:00
3:20              3:35 

What is the best way to determine over speed periods (speed barrier is defined) ? My first idea was loading the table into an array, and then iterate over array to find these periods:

(Pseudo C# code)

bool isOverSpeed = false;

for (int i =0;i<arr.Length;i++)
{
if (!isOverSpeed)
    if (arr[i].Velocity > speedBarrier)
        {
            #insert the first record into another array.
            isOverSpeed = true;
        }
if(isOverSpeed)

    if (arr[i].Velocity < speedBarrier)
          {
          #insert the record into that array
          isOverSpeed = false;
          }

}

It works, but somewhat “not very effectively”. Is there a “smarter” way, such as a T-SQL query or another algorithm to do this?

  • 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-15T04:26:47+00:00Added an answer on May 15, 2026 at 4:26 am

    You can achieve this by using CTE (Common Table Expressions).

    The query below works against the Adventure Works demo table of SQL Server (the “speed limit” being 7).

    This is strongly inspired by another question on SO: GROUP BY for continuous rows in SQL.

    with CTE as (
        select
            ROW_NUMBER() over(order by SalesTaxRateID) as RowNo
            , *
        from
            Sales.SalesTaxRate
    )
    , MyLogGroup as (
        select
            l.*
            ,(select
                  max(SalesTaxRateID)
              from
                  CTE c
              where
                  not exists (select * from CTE
                                  where RowNo = c.RowNo-1
                                  and TaxRate > 7
                                  and c.TaxRate > 7)
                  and c.SalesTaxRateID <= l.SalesTaxRateID) as GroupID
        from
            Sales.SalesTaxRate l)
    select
        min(SalesTaxRateID) as minimum
        , max(SalesTaxRateID) as maximum
        , avg(TaxRate)
    from
        MyLogGroup
    group by
        GroupID
    having
        min(TaxRate) > 7
    order by
        minimum
    

    Something along these lines should suit you:

    with CTE as (
        select
            ROW_NUMBER() over(order by [Time]) as RowNo
            , *
        from
            <table_name>
    )
    , MySpeedGroup as (
        select
            s.*
            ,(select
                  max([Time])
              from
                  CTE c
              where
                  not exists (select * from CTE
                                  where RowNo = c.RowNo-1
                                  and Velocity > <speed_limit>
                                  and c.Velocity > <speed_limit>)
                  and c.[Time] <= s.[Time]) as GroupID
        from
            <table_name> l)
    select
        min([Time]) as minimum
        , max([Time]) as maximum
        , avg([Velocity]) -- don't know if you want this
    from
        MySpeedGroup
    group by
        GroupID
    having
        min(Velocity) > <speed_limit>
    order by
        minimum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a pretty basic question, but just something that I wanted to
It doesn't really have to add newlines, just something readable. Anything better than this?
I have an interesting situation that my usually clever mind hasn't been able to
So I've just come across something interesting, and I don't know if it's been
I just saw something very interesting in vk.com Basically in profile page the right
I was looking through a code tutorial just now, and found something interesting --
Ok, I don't know if this is possible, just something wrong with my code...
this seems straightforward enough, I don't know if it's a bug, or just something
I've been programming in Java for a while, and I've just come across this
I'm doing some ASP.NET development in VS and have just found an interesting little

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.