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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:45:59+00:00 2026-05-31T09:45:59+00:00

I have the following problem to solve and I can’t seem to be able

  • 0

I have the following problem to solve and I can’t seem to be able to come up with an algorithm yet, nevermind an actual solution.

I have a table of similar structure/data as the following, where IDs are not always in sequence for the same Ticker/QuouteType:

ID      Ticker PriceDateTime    QuoteType OpenPrice HighPrice LowPrice ClosePrice
------- ------ ---------------- --------- --------- --------- -------- ----------
2036430 ^COMP  2012-02-10 20:50 95/Minute 2901.57   2905.04   2895.37  2901.71
2036429 ^COMP  2012-02-10 19:15 95/Minute 2909.63   2910.98   2899.95  2901.67
2036428 ^COMP  2012-02-10 17:40 95/Minute 2905.9    2910.27   2904.29  2909.64
2036427 ^COMP  2012-02-10 16:05 95/Minute 2902      2908.29   2895.1   2905.89
2036426 ^COMP  2012-02-09 21:00 95/Minute 2926.12   2928.01   2925.53  2927.21

The information I need to extract from this data is the following:

  • How many consecutive rows are there? Counting downwards from the most recent (as recorded in PriceDateTime), looking at ClosePrice?

IE: For the current example the answer should be 2. ClosePrice (row 1) = 2901.71 which is greater than ClosePrice (row 2) = 2901.67 but lower than ClosePrice (row 3) = 2909.64. As such, looking back from the most recent price, we have 2 rows that “go in the same direction”.

Of course I have to do this across a lot of other names, so speed is quite important.

PS: Thank you all for your help, I’ve drawn inspiration from all your answers when building the final procedure. You’re all very kind!

  • 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-31T09:46:00+00:00Added an answer on May 31, 2026 at 9:46 am

    Try this: (I have simplified the test data I’m using as it only requires 2 columns to demonstrate the logic).

    CREATE TABLE #Test (PriceDateTime DATETIME, ClosePrice DECIMAL(6, 2))
    INSERT #Test VALUES 
    ('20120210 20:50:00.000', 2901.71),
    ('20120210 19:15:00.000', 2901.67),
    ('20120210 17:40:00.000', 2900.64),
    ('20120210 16:05:00.000', 2905.89),
    ('20120209 21:00:00.000', 2927.21)
    
    -- FIRST CTE, JUST DEFINES A VIEW GIVING EACH ENTRY A ROW NUMBER
    ;WITH CTE AS
    (   SELECT  *,
                ROW_NUMBER() OVER(ORDER BY PriceDateTime DESC) [RowNumber]
        FROM    #Test
    ), 
    -- SECOND CTE, ASSIGNES EACH ENTRY +1 OR -1 DEPENDING ON HOW THE VALUE HAS CHANGED COMPARED TO THE PREVIOUS RECORD
    CTE2 AS
    (   SELECT  a.*, SIGN(a.ClosePrice - b.ClosePrice) [Movement]
        FROM    CTE a
                LEFT JOIN CTE b
                    ON a.RowNumber = b.RowNumber - 1
    ), 
    -- THIRD CTE, WILL LOOP THROUGH THE DATA AS MANY TIMES AS POSSIBLE WHILE THE PREVIOUS ENTRY HAS THE SAME "MOVEMENT"
    CTE3 AS
    (   SELECT  *, 1 [Recursion]
        FROM    CTE2
        UNION ALL
        SELECT  a.PriceDateTime, a.ClosePrice, a.RowNumber, a.Movement, b.Recursion + 1
        FROM    CTE2 a
                INNER JOIN CTE3 b
                    ON a.RowNumber = b.RowNumber - 1
                    AND a.Movement = b.Movement
    )
    
    SELECT  MAX(Recursion) + 1 -- ADD 1 TO THE RECORD BECAUSE THERE WILL ALWAYS BE AT LEAST TWO ROWS
    FROM    CTE3
    WHERE   RowNumber = 1 -- LATEST ENTRY
    
    DROP TABLE #Test
    

    I’ve tried to comment the answer to explain as I go. If anything is not clear from the comments let me know and I will try and explain further

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

Sidebar

Related Questions

i wonder how i can solve the following problem. i have a horizontal scrollbar
I have another problem that I can't solve I have a following code that
I have the following problem to solve: Let's say there is a table A
after several attempts, I can not solve this problem. I have the following mysql
I have the following problem that the standard library doesn't solve well, and I'm
i'm not sure how to solve the following problem: i have a triangle with
I am trying to solve the following problem. Lets say you have the the
Simplified, I have to solve the following problem: You have a 2-dimensional array filled
I have the following cyclic dependency problem I am trying to solve: typedef std::map<int,
I have the following problem. I have a set of elements that I can

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.