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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:45:41+00:00 2026-05-28T18:45:41+00:00

RecordNo Speed ——– —– 1 0 2 0 3 0 4 0 5 23

  • 0
RecordNo    Speed  
--------    -----
1           0
2           0
3           0
4           0
5           23
6           66
7           48
8           0
9           31
10          0
11          34
12          23

The above data shows the speed of vehicle over a time period, given the above data I need to achieve the result below:

RecordNo    Speed   LastAcceleration
--------    -----   ----------------
1           0       0
2           0       0
3           0       0
4           0       0
5           23      23
6           66      66
7           48      66
8           0       66
9           31      31
10          0       31
11          34      34
12          23      34

The code below is almost there but falls over on Recordno 8:

select
curr.recordno,curr.speed 
,CASE WHEN curr.speed >= ISNULL(prev.speed,0) THEN curr.speed
    ELSE (
            SELECT MAX(speed) FROM speedtest 
            WHERE recordno between (CASE WHEN curr.speed >= prev.speed then curr.recordindex else prev.recordno end ) and curr.recordno 
          ) 
END as LastAcceleration
From speedtest prev RIGHT JOIN speedtest curr
on prev.vrm = curr.vrm
and prev.recordno+1 = curr.recordno
order by curr.recordno

I think I’ve been staring at this one too long. I’ve tried self-joins with correlated sub-queries but think I’m missing something obvious? This is for a 2008 project so doesn’t have to work with any versions prior to that. Any help would be appreciated.

  • 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-28T18:45:42+00:00Added an answer on May 28, 2026 at 6:45 pm

    Here you go! You basically missed the part where you need to check for the latest acceleration whenever its not accelerating (the not exists portion of my query moreorless)

    create table #t (recordno int, speed int)
    insert into #t
    select 1,           0
    union select 2,           0
    union select 3,           0
    union select 4,           0
    union select 5,           23
    union select 6,           66
    union select 7,           48
    union select 8,           0
    union select 9,           31
    union select 10,          0
    union select 11,          34
    union select 12,          23
    
    ;with acc as (
    select
        t1.*,
        case when t1.speed >= isnull(t2.speed, 0) then t1.speed else -1 end as 'accel'
    from
        #t t1
        left join #t t2
            on  t1.recordno = t2.recordno + 1
    )
    
    select 
        a.recordno, a.speed, 
        case 
            when a.accel >= 0 then a.accel 
            else (
                select accel 
                from acc a1 
                where a1.recordno < a.recordno and a1.accel >= 0 
                    and not exists (
                        select 1 
                        from acc a2 
                        where a2.recordno < a.recordno and a2.accel >= 0 and a2.recordno > a1.recordno)
                )
        end
    from
        acc a
    
    drop table #t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying the following: CREATE TABLE Table1 ( RecordNo autonumber, --error here! PersonId varchar(50),
In my code, I want to view all data from a CSV in table
I am having a really hard time updating and deleting many to many relationships
Using SQL Server. Table1 RecordNo ID --------------- 2 001 3 002 4 003 ....
Im new on Dxpreince 10.(and don't think im expert on this) I wrote simple
Using VB 6 and Access Database My query. Creating a table sql4 = CREATE
I have problem executing below stored procedure. I am getting error of ORA-00932: inconsistent
I am trying to open an preview.aspx page in a seperate window from INSIDE
Can someone please help me with MySQL Trigger Lets say I have two Tables
Using classic asp, I am trying to query a SQL Server database like so:

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.