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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:35:33+00:00 2026-06-03T21:35:33+00:00

I’m working on a query that I think I need recursion for. In this

  • 0

I’m working on a query that I think I need recursion for. In this case, I can’t quite figure out the base case and how to structure it. I have stock market data, and I want to compare moving averages ordered desc by date, and row number/ partition by the sign of the difference between two moving averages (as well as on symbol and period values as per cte). I’m using a column of 0 and 1 to help determine the partitions. With a standard partition, if a group of rows has a value of 0, then the results switch to 1 then back to 0, the 0’s are all partitioned together. what I need in my result set is a new partition for each change from 0 to 1 or vice versa. I added the code as I have it now (doesn’t return 2nd column, that’s added to show my desired output) and some sample data I have thus far below, again with the 2nd column (RowNInner) being the column I’m trying to achieve:

WITH allData AS
(
    SELECT t1.Symbol, t.TradingDate AS TradeDate, t1.Period AS FastPeriod, t2.Period AS SlowPeriod, 
    t1.Value AS FastValue, t2.Value AS SlowValue, (t1.Value - t2.Value) AS SlowFastDiff,
    CASE 
        WHEN (t1.Value - t2.Value) < 0 THEN 0
        ELSE 1
    END AS ChgSign
    FROM tblTradingDays t RIGHT JOIN tblDailySMA t1 ON t.TradingDate = t1.TradeDate JOIN tblDailySMA t2 ON 
        t1.Symbol = t2.Symbol AND t1.TradeDate = t2.TradeDate
    WHERE t1.Period < t2.Period AND t1.Symbol < 'AC'
)

SELECT DENSE_RANK() OVER (ORDER BY Symbol, FastPeriod, 
    SlowPeriod) AS RowNOuter, Symbol, TradeDate, FastPeriod, SlowPeriod, FastValue, SlowValue, 
    SlowFastDiff, ChgSign
FROM allData
ORDER BY Symbol, FastPeriod, SlowPeriod, TradeDate DESC, RowNInner

Sample result set (2nd column is what I seek):

RowNOuter   RowNInner     Symbol    TradeDate   FastPeriod  SlowPeriod  FastValue   SlowValue   SlowFastDiff    ChgSign
1           1             A         5/11/2012   5           10          40.05       41.166      -1.116           0
1           2             A         5/10/2012   5           10          40.362      41.477      -1.115           0
1           3             A         5/9/2012    5           10          40.856      41.702      -0.846           0
1           1             A         5/8/2012    5           10          42.018      41.78       0.238            1
1           2             A         5/7/2012    5           10          42.282      41.821      0.461            1
1           1             A         5/6/2012    5           10          41.542      41.797      -0.255           0
1           2             A         5/5/2012    5           10          41.36       41.778      -0.418           0
  • 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-03T21:35:36+00:00Added an answer on June 3, 2026 at 9:35 pm

    If I am not missing anything, this can be solved without a recursive CTE.

    The idea is as follows:

    1. Rank the rows within every partition of (Symbol, FastPeriod, SlowPeriod) in the descending order of TradeDate (I guess).

    2. Rank the rows within everty partition of (Symbol, FastPeriod, SlowPeriod, ChgSign) (again, in the descending order of TradeDate).

    3. Find the difference between the two rankings. This will be an additional criterion for the final partitioning.

    4. Now you can just rank the rows once again, partitioning them by (Symbol, FastPeriod, SlowPeriod, ChgSign, RankDiff).

    Here’s what the resulting query might look like:

    WITH allData AS
    (
        SELECT
            t1.Symbol,
            TradeDate    = t.TradingDate,
            FastPeriod   = t1.Period,
            SlowPeriod   = t2.Period,
            FastValue    = t1.Value,
            SlowValue    = t2.Value,
            SlowFastDiff = (t1.Value - t2.Value),
            ChgSign      = CASE WHEN t1.Value < t2.Value THEN 0 ELSE 1 END
        FROM tblTradingDays t
            RIGHT JOIN tblDailySMA t1 ON t.TradingDate = t1.TradeDate
            INNER JOIN tblDailySMA t2 ON t1.Symbol     = t2.Symbol
                                     AND t1.TradeDate  = t2.TradeDate
        WHERE t1.Period < t2.Period
          AND t1.Symbol < 'AC'
    )
    ,    partitioned AS (
        SELECT
            RowOuter = DENSE_RANK() OVER (ORDER BY Symbol, FastPeriod, SlowPeriod),
            RankDiff = ROW_NUMBER() OVER (
                           PARTITION BY Symbol, FastPeriod, SlowPeriod
                           ORDER BY TradeDate DESC
                       )
                     - ROW_NUMBER() OVER (
                           PARTITION BY Symbol, FastPeriod, SlowPeriod, ChgSign
                           ORDER BY TradeDate DESC
                       ),
            Symbol,
            TradeDate,
            FastPeriod,
            SlowPeriod,
            FastValue,
            SlowValue, 
            SlowFastDiff,
            ChgSign
        FROM allData
    )
    SELECT
        RowOuter,
        RowInner = ROW_NUMBER() OVER (
           PARTITION BY Symbol, FastPeriod, SlowPeriod, ChgSign, RankDiff
           ORDER BY TradeDate DESC
        ),
        Symbol,
        TradeDate,
        FastPeriod,
        SlowPeriod,
        FastValue,
        SlowValue, 
        SlowFastDiff,
        ChgSign
    FROM partitioned
    ORDER BY Symbol, FastPeriod, SlowPeriod, TradeDate DESC, RowNInner
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.