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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:05:09+00:00 2026-06-13T11:05:09+00:00

I have multiple CTEs and I want to retrieve some information from a couple

  • 0

I have multiple CTEs and I want to retrieve some information from a couple of them into next CTE.

So, I have this information from one of the CTEs:

PeriodID  StarDate
1         2006-01-01
2         2007-04-25
3         2008-08-16
4         2009-12-08
5         2011-04-017

and this from other:

RecordID  Date
100       2007-04-15
101       2008-05-21
102       2008-06-06
103       2008-07-01
104       2009-11-12

And I need to show in next one:

RecordID  Date        PeriodID
100       2007-04-15  1
101       2008-05-21  2
102       2008-06-06  2
103       2008-07-01  2
104       2009-11-12  3

I can use some case/when statement to define if date of record is in period 1,2,3,4 or 5 but it some situation I can have different numbers of periods return from the first CTE.

Is there a way to do this in the above context?

  • 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-13T11:05:10+00:00Added an answer on June 13, 2026 at 11:05 am

    You can have multiple CTEs defined as follows, and then select from and join them as you would any other table.

    with cte1 as (select * ...),
    cte2 as (select * ...)
        select 
            cte2.*, 
            periodid 
        from cte2
    cross apply 
            (select top 1 * from cte1 where cte2.recorddate> cte1.startdate order by startdate desc) v
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple NxM matrices and want to select one of them by a
I have multiple domains on my server. I want to redirect all of them
I have multiple arrays with string values and I want to compare them and
I have multiple divs on my page.I want to copy selected text from div1
I have multiple classes that all derive from a base class, now some of
I have multiple <li> elements which contain multiple <div> elements. Some of them have
I have multiple html files and some of them have some blank lines, I
I have multiple input tag submitted from previous page, say step1.php like this :
I have multiple UpdatePanels on a webpage, one inside UserControl (Purple ColorBox as show
I have multiple forms for lots of products on my page and want to

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.