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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:00:31+00:00 2026-05-12T17:00:31+00:00

In SQL Server 2005 I have a table with data that looks something like

  • 0

In SQL Server 2005 I have a table with data that looks something like this:

WTN------------Date  
555-111-1212  2009-01-01  
555-111-1212  2009-01-02  
555-111-1212  2009-01-03  
555-111-1212  2009-01-15  
555-111-1212  2009-01-16  
212-999-5555  2009-01-01  
212-999-5555  2009-01-10  
212-999-5555  2009-01-11 

From this I would like to extract WTN, Min(Date), Max(Date) the twist is I would like to also break whenever there is a gap in the dates, so from the above data, my results should look like:

WTN------------ MinDate---- MaxDate  
555-111-1212   2009-01-01  2009-01-03  
555-111-1212   2009-01-15  2009-01-16  
212-999-5555   2009-01-01  2009-01-01  
212-999-5555   2009-01-10  2009-01-11  
  1. How can I do this in a SQL Select/ Group By?
  2. Can this be done without a table or list enumerating the values I want to identify gaps in (Dates here)?
  • 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-12T17:00:32+00:00Added an answer on May 12, 2026 at 5:00 pm

    Why is everyone so dead set against using a table for this kind of thing? A table of numbers or a calendar table takes up such little space and is probably in memory if referenced enough anyway. You can also derive a numbers table pretty easily on the fly using ROW_NUMBER(). Using a numbers table can help with the understanding of the query. But here is a not-so-straightforward example, a trick I picked up from Plamen Ratchev a while back, hope it helps.

    DECLARE @wtns TABLE
    (
        WTN    CHAR(12),
        [Date] SMALLDATETIME
    );
    
    INSERT @wtns(WTN, [Date])
              SELECT '555-111-1212','2009-01-01'
    UNION ALL SELECT '555-111-1212','2009-01-02'
    UNION ALL SELECT '555-111-1212','2009-01-03'
    UNION ALL SELECT '555-111-1212','2009-01-15'
    UNION ALL SELECT '555-111-1212','2009-01-16'
    UNION ALL SELECT '212-999-5555','2009-01-01'
    UNION ALL SELECT '212-999-5555','2009-01-10' 
    UNION ALL SELECT '212-999-5555','2009-01-11';
    
    WITH x AS
    (
        SELECT
            [Date],
            wtn,
            part = DATEDIFF(DAY, 0, [Date]) 
            + DENSE_RANK() OVER
            (
                PARTITION BY wtn
                ORDER BY [Date] DESC
            )
        FROM @wtns
    )
    SELECT 
        WTN, 
        MinDate = MIN([Date]),
        MaxDate = MAX([Date])
    FROM
        x
    GROUP BY 
        part,
        WTN
    ORDER BY
        WTN DESC,
        MaxDate;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 296k
  • Answers 296k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can still create a UNIQUE constraint on the name… May 13, 2026 at 7:09 pm
  • Editorial Team
    Editorial Team added an answer http://msdn.microsoft.com/en-us/library/yh598w02.aspx The using statement ensures that Dispose is called even… May 13, 2026 at 7:09 pm
  • Editorial Team
    Editorial Team added an answer yes, once you merged c to Master, you can delete… May 13, 2026 at 7:09 pm

Related Questions

Question In SQL Server 2005, I have a table with images (data type: image).
I have a table with an image column in SQL Server 2000. I need
In SQL Server 2005, I have a table of input coming in of successful
I am using SQL Server 2005. I have a table with a text column

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.