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

The Archive Base Latest Questions

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

I’m trying to generate a data set, and I think recursion is required, but

  • 0

I’m trying to generate a data set, and I think recursion is required, but I can’t quite adapt the numerous examples I’ve found to my purposes. The end result I want is fairly simple to describe.

Two tables are involved: the first with trading pricing for stocks with the relevant fields TradeDate, Symbol, and Clse (closing price), and a table with trading days listed. I’d like to partition the pricing data by symbol, ordered by date, but I’d like the partition/ row numbering to break if a trade date is missing from the pricing table, since this data comes in from a web service that is a little unpredictable at times.

My first attempt was a simple enough select query that had a WHERE clause that called a udf I wrote to see if there was continuous data for that symbol/ date combination. It worked (it ran) and the night I ran it, after 7 hours it was almost through the symbols starting with ‘A.’

EDIT:
This post of mine and the response a little while back SQL server select query help for stored procedure needed on SO was helpful conceptually, but my current use of the concept requires a little more (table join, partitioning/ row numbering etc)

If it helps understand the problem, I want to use what’s returned to calculate various aggregations per symbol, trade date combination that use the past data points. Example: 5 period moving average of closing price for a symbol/ trade date would be the average of the five closing prices (clse) that have a trade date <= the date being calculated for. So using the sample data below from the first table, I want to return 9.02 for symbol ‘A’, 3 periods, trade date 1/3/12. If data is missing for one of the calculations I want null.

This result set I want would be a simple PARTITION select query if it weren’t for having to check for missing dates. So here is some sample data of the tables involved and my goal result set:

tblDailyPricingAndVol

TradeDate Symbol Clse
1/1/12    A      9.01
1/2/12    A      9.05
1/3/12    A      8.99
1/5/12    A      9.03
1/1/12    B      10.05
1/4/12    B      10.11
1/5/12    B      10.03

tblTradingDays

TradingDate
1/1/12
1/2/12
1/3/12
1/4/12
1/5/12

Goal result set:

RowNumber TradeDate Symbol Clse
1         1/1/12    A      9.01
2         1/2/12    A      9.05
3         1/3/12    A      8.99
1         1/5/12    A      9.03
1         1/1/12    B      10.05
1         1/4/12    B      10.11
2         1/5/12    B      10.03

Hope that makes sense. Any help would be appreciated. I think I’ll see recursive cte’s much more clearly if I can get one running as expected on my own data. Thanks.

  • 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-31T01:45:29+00:00Added an answer on May 31, 2026 at 1:45 am

    I think combining dense_rank() and row_number() will work here:

    ; with cte as (
        select d.TradingDate,p.Symbol,p.Clse
        , r=DENSE_RANK()over(order by d.TradingDate)
        , r1=row_number()over(partition by p.Symbol order by p.TradeDate)
        from tblTradingDays d
        inner join tblDailyPricingAndVol p on d.TradingDate=p.TradeDate
    )
    select
    RowNumber=row_number()over(partition by Symbol, (r-r1) order by TradingDate)
    , TradingDate,Symbol,Clse
    from cte
    go
    

    Result:

    enter image description here

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

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

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.