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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:51:53+00:00 2026-06-17T20:51:53+00:00

This query returns all days between date range defined with variables and result set

  • 0

This query returns all days between date range defined with variables and result set is 31 distinct values. But, recursive CTE works in such a way that first query executed only once, and second query works on result set created before. So, it seems that there will be duplicates, but it returned distinct result set. Whether CTE apply DISTINCT clause internally or is it something else? How I get DISTINCT values?

DECLARE 
    @DateFrom DATE = '20130101' ,
    @DateTo DATE = '20130131'

WITH Days
AS ( SELECT CAST(@DateFrom AS DATETIME) AS dt
     UNION ALL
     SELECT DATEADD(dd, 1, dt)
     FROM Days s
     WHERE DATEADD(dd, 1, dt) <= CAST(@DateTo AS DATETIME)
   )

SELECT dt 
FROM Days
  • 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-17T20:51:54+00:00Added an answer on June 17, 2026 at 8:51 pm

    Each time a CTE recurses, it uses the rows produced by the previous recursion (or the initial set of rows if this is the first time it recurses).

    So, for your query, the first run produces a single row – '20130101' converted to a datetime.

    For the first recursive step, we take all the rows produced by the last recursion or initial set ('20130101') and use it to produce one new row, which contains '20130102'.

    For the next recursive step, we take all rows produced by the last recursion ('20130102') and use it to produce one new row ('20130103')

    For the next recursive step, we take all rows produced by the last recursion ('20130103') and use it to produce one new row ('20130104')

    I don’t see anywhere where duplicates are going to arise.

    Read more here:

    The semantics of the recursive execution is as follows:

    1. Split the CTE expression into anchor and recursive members.

    2. Run the anchor member(s) creating the first invocation or base result set (T0).

    3. Run the recursive member(s) with Ti as an input and Ti+1 as an output.

    4. Repeat step 3 until an empty set is returned.

    5. Return the result set. This is a UNION ALL of T0 to Tn.

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

Sidebar

Related Questions

I have this query which returns a result set with Date(column) as nvarchar datatype.
Can anyone explain why this query returns an empty result. SELECT * FROM (`bookmarks`)
I'm gettintg crazy with the result of this query This query returns 29.970 rows
Mysql query returns result like this collector amount name 1 0.00 gihan 1 2000.00
how set distinct to this query return _dataContext.ProjectPictures.Include(Projects).Include(ProjectPictureTypes).Where(p => p.ProjectPictureTypes.Id == 1).ToList(); i need
This is my query i need the tooltip for all the days eg: when
I have this query that returns the results by ordering it by focus.name ASC.
I have a LINQ query that returns some object like this... var query =
I have this query to count views in a table, by date. It obviously
I can't seem to find anything to make a date range query using Mongoid/Rails.

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.