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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:37:30+00:00 2026-05-24T15:37:30+00:00

SQL Server 2008: I’m having a tough time translating my pseudo SQL to actual

  • 0

SQL Server 2008: I’m having a tough time translating my pseudo SQL to actual execution. In short, I have a data set that has:

id, startdate, enddate

so, for example:

1,1/1/2010,2/1/2010
1,3/1/2010,3/15/2010
2,4/1/2010,6/1/2010
2,5/1/2010,5/15/2010
2,7/1/2010,7/15/2010

Where each ID could be listed multiple times and the dates could overlap.

I need to:

a) Get the consecutive days for each ID. So, for example:

1 = 1/1/2010 thru 2/1/2010 + 3/1/2010 through 3/15/2010 = 43
2 = 4/1/2010 thru 6/1/2010 + 7/1/2010 through 7/15/2010 (note: the 5/1/10 thru 5/15 was omitted because it overlapped)

My initial thought was to write a sql query that would do this:

Date, ID, Active

I’m pulling data for an entire year, so I would have a date/ID entry for each ID in the table. The “active” would be a 1 or 0 depending on whether that ID was “on” for that particular day.

Then I could group that up and get my “distinct” cumulative for the year.

Problem is, that will create a table with over 300,000,000 million records, and I just can’t imagine there isn’t a better way to do this.

Any advice would be greatly appreciated.

  • 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-24T15:37:31+00:00Added an answer on May 24, 2026 at 3:37 pm

    If I understand your question correctly, this will give you your results

    /*setup data*/
    CREATE TABLE #dates 
    (
            id INT,
            startdate DATETIME,
            enddate DATETIME
    )
    
    INSERT INTO #dates 
    SELECT 1,'1/1/2010','2/1/2010'
    UNION ALL 
    SELECT 1,'3/1/2010','3/15/2010'
    UNION ALL 
    SELECT 2,'4/1/2010','6/1/2010'
    UNION ALL 
    SELECT 2,'5/1/2010','5/15/2010'
    UNION ALL 
    SELECT 2,'7/1/2010','7/15/2010'
    
    
    /* this is our "tally-table" maybe make this static*/
    CREATE TABLE #numbers 
    (
        NUM INT PRIMARY KEY CLUSTERED 
    )
    
    ;WITH Nbrs ( n ) AS (
            SELECT 1 UNION ALL
            SELECT 1 + n FROM Nbrs WHERE n < 500 )
        INSERT INTO #numbers 
    SELECT n FROM Nbrs
        OPTION ( MAXRECURSION 500 )
    
    /*first we get our full range*/
    ;WITH fullrange
    AS
    (
        SELECT D.id, DATEADD(dd,N.num-1,D.startdate) AS dte
        FROM #dates D
            INNER JOIN #numbers N 
                ON N.num <= DATEDIFF(dd,D.startdate, D.enddate)
                /*By joining to the numbers/tally table we can extrapolate the full range of dates like you alluded to in your considered approach*/
    )
    /*then we aggregate*/
    SELECT id, COUNT(DISTINCT dte) AS active --Now we can just count distinct dates for each id
    FROM fullrange 
    GROUP BY id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server 2008, I have a strongly typed data set with a table:
Does SQL Server 2008 have a a data-type like MySQL's enum ?
In Sql Server 2008 I have a DB table, and the data includes a
SQL Server 2008 has new geo data types. One thing I am wondering though
SQL Server 2008 Database Question. I have 2 tables, for arguments sake called Customers
In sql server 2008, I have the following query: select c.title as categorytitle, s.title
SQL Server 2008. Is it possible to create Change Data Capture (or Change Tracking)
Sql server 2008 R2 I have two queries DECLARE @title NVARCHAR(500) = 'Finite AND
SQL Server 2008 provides the ability to script data as Insert statements using the
SQL Server 2008 I almost have, I think, what I'm looking to do. I'm

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.