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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:22:37+00:00 2026-06-13T14:22:37+00:00

I am stumbled upon a problem, when I need a query which will produce

  • 0

I am stumbled upon a problem, when I need a query which will produce a list of speeding time frames.

Here is the data example

[idgps_unit_location]   [dt]    [idgps_unit]    [lat]   [long]  [speed_kmh]
26  10/18/2012 18:53    2   47  56  30
27  10/18/2012 18:53    2   49  58  31
28  10/18/2012 18:53    2   28  37  15
29  10/18/2012 18:54    2   56  65  33
30  10/18/2012 18:54    2   152 161 73
31  10/18/2012 18:55    2   134 143 64
32  10/18/2012 18:56    2   22  31  12
36  10/18/2012 18:59    2   98  107 47
37  10/18/2012 18:59    2   122 131 58
38  10/18/2012 18:59    2   91  100 44
39  10/18/2012 19:00    2   190 199 98
40  10/18/2012 19:01    2   194 203 101
41  10/18/2012 19:02    2   182 191 91
42  10/18/2012 19:03    2   162 171 78
43  10/18/2012 19:03    2   174 183 83
44  10/18/2012 19:04    2   170 179 81
45  10/18/2012 19:05    2   189 198 97
46  10/18/2012 19:06    2   20  29  10
47  10/18/2012 19:07    2   158 167 76
48  10/18/2012 19:08    2   135 144 64
49  10/18/2012 19:08    2   166 175 79
50  10/18/2012 19:09    2   9   18  5
51  10/18/2012 19:09    2   101 110 48
52  10/18/2012 19:09    2   10  19  7
53  10/18/2012 19:10    2   32  41  20
54  10/18/2012 19:10    1   54  63  85
55  10/19/2012 19:11    2   55  64  50

I need a query that would convert this table into the following report that shows frames of time when speed was >80:

[idgps_unit]    [dt_start]  [lat_start] [long_start]    [speed_start]   [dt_end]    [lat_end]   [long_end]  [speed_end] [speed_average]
2   10/18/2012 19:00    190 199 98  10/18/2012 19:02    182 191 91  96.66666667
2   10/18/2012 19:03    174 183 83  10/18/2012 19:05    189 198 97  87
1   10/18/2012 19:10    54  63  85  10/18/2012 19:10    54  63  85  85

Now, what have I tried? I tried putting this into separate tables, queries and do some joins… Nothing works and I am very frustrated… I am not even sure if this could be done via the query. Asking for the expert 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-13T14:22:38+00:00Added an answer on June 13, 2026 at 2:22 pm

    You were right, it is fairly tricky, but I think I’ve managed it:

    SELECT  s.idgps_unit,
            MIN(s.dt) AS DT_Start,
            MIN(CASE WHEN s.RowNumber = 1 THEN s.Lat END) AS Lat_Start,
            MIN(CASE WHEN s.RowNumber = 1 THEN s.Long END) AS Long_Start,
            MIN(CASE WHEN s.RowNumber = 1 THEN s.Speed_kmh END) AS Speed_Start,
            MAX(s.dt) AS dt_end,
            MIN(CASE WHEN s.RowNumber = MaxRowNumber THEN s.Lat END) AS Lat_End,
            MIN(CASE WHEN s.RowNumber = MaxRowNumber THEN s.Long END) AS Long_End,
            MIN(CASE WHEN s.RowNumber = MaxRowNumber THEN s.Speed_kmh END) AS Speed_End,
    
            AVG(Speed_kmh) AS Speed_Average
    FROM    (   SELECT  T.*,
                        @i:= CASE WHEN Speed_Kmh > 80 AND @b = 0 THEN @i + 1 ELSE @i END AS IntervalID,
                        @r:= CASE WHEN Speed_Kmh > 80 AND @b = 0 THEN 1 ELSE @r + 1 END AS RowNumber,
                        @b:= CASE WHEN Speed_Kmh> 80 THEN 1 ELSE 0 END AS IntervalCheck
                FROM    T,
                        (SELECT @i:= 0) i,
                        (SELECT @r:= 0) r,
                        (SELECT @b:= 0) b
                ORDER BY dt, idgps_unit_location
            ) s
            INNER JOIN
            (   SELECT  IntervalID, MAX(RowNumber) AS MaxRowNumber
                FROM    (   SELECT  T.*,
                                    @i:= CASE WHEN Speed_Kmh > 80 AND @b = 0 THEN @i + 1 ELSE @i END AS IntervalID,
                                    @r:= CASE WHEN Speed_Kmh > 80 AND @b = 0 THEN 1 ELSE @r + 1 END AS RowNumber,
                                    @b:= CASE WHEN Speed_Kmh> 80 THEN 1 ELSE 0 END AS IntervalCheck
                            FROM    T,
                                    (SELECT @i:= 0) i,
                                    (SELECT @r:= 0) r,
                                    (SELECT @b:= 0) b
                            ORDER BY dt, idgps_unit_location
                        ) d
                WHERE   IntervalCheck = 1
                GROUP BY IntervalID
            ) MaxInt
                ON MaxInt.IntervalID = s.IntervalID
    WHERE   s.IntervalCheck = 1
    GROUP BY s.IntervalID, s.idgps_unit;
    

    The key is in this part:

    SELECT  T.*,
            @i:= CASE WHEN Speed_Kmh > 80 AND @b = 0 THEN @i + 1 ELSE @i END AS IntervalID,
            @r:= CASE WHEN Speed_Kmh > 80 AND @b = 0 THEN 1 ELSE @r + 1 END AS RowNumber,
            @b:= CASE WHEN Speed_Kmh> 80 THEN 1 ELSE 0 END AS IntervalCheck
    FROM    T,
            (SELECT @i:= 0) i,
            (SELECT @r:= 0) r,
            (SELECT @b:= 0) b
    ORDER BY dt, idgps_unit_location
    

    Each time a row is encountered where the speed is over it sets the variable @b to 1, if this variable was 0 before it assigns the row a new intervalID, if it does this it begins numbering the row at 1 again, so you end up with something like this:

    [idgps_unit_location]   [dt]                [idgps_unit]    [lat]   [long]  [speed_kmh] [IntervalID]    RowNumber   IntervalCheck
    37                      10/18/2012 18:59    2               122     131     58          1               1           0
    38                      10/18/2012 18:59    2               91      100     44          1               2           0
    39                      10/18/2012 19:00    2               190     199     98          2               1           1
    40                      10/18/2012 19:01    2               194     203     101         2               2           1
    41                      10/18/2012 19:02    2               182     191     91          2               3           1
    42                      10/18/2012 19:03    2               162     171     78          2               4           0
    43                      10/18/2012 19:03    2               174     183     83          3               1           1
    

    You then need to elimate all rows where the speed is under 80 (WHERE IntervalCheck = 1), and finally you can use aggregate functions along with CASE to find the rows where RowNumber is 1 (the first row of speeding), or the highest rownumber for that interval (the last row of speeding). The join at the end simply repeats the process to find what the maximum rownumber is for each intervalID.

    Example on SQL Fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hy, I got an intersting problem which I stumbled upon. When I double-click a
Aside from scalability issues, has anyone here actually stumbled upon a web-development problem where
I stumbled upon a rather exotic c++ namespace problem: condensed example: extern C {
I stumbled upon a problem where i need an instance of the class inside
At my workplace, we just stumbled upon the problem where we need to create
I am currently re-designing an application and stumbled upon a problem serializing some data.
I have stumbled upon a problem when calling a nested Async which happens to
In our project we've stumbled upon the following problem: we need to provide our
I stumbled upon this problem and it took my a while to realise what
I stumbled upon a problem of how to make work together acts_as_taggable (on steroids)

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.