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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:39:35+00:00 2026-06-07T09:39:35+00:00

Im trying to write a function which identifies groups of dates, and measures the

  • 0

Im trying to write a function which identifies groups of dates, and measures the size of the group.

I’ve been doing this procedurally in Python until now but I’d like to move it into SQL.

for example, the list

Bill 01/01/2011 
Bill 02/01/2011 
Bill 03/01/2011 
Bill 05/01/2011 
Bill 07/01/2011 

should be output into a new table as:

Bill 01/01/2011  3 
Bill 02/01/2011  3 
Bill 03/01/2011  3 
Bill 05/01/2011  1 
Bill 07/01/2011  1

Ideally this should also be able to account for weekends and public holidays – the dates in my table will aways be Mon-Fri (I think I can solve this by making a new table of working days and numbering them in sequence). Someone at work suggested I try a CTE. Im pretty new to this, so I’d appreciate any guidance anyone could provide! 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-06-07T09:39:36+00:00Added an answer on June 7, 2026 at 9:39 am

    You can do this with a clever application of window functions. Consider the following:

    select name, date, row_number() over (partition by name order by date)
    from t
    

    This adds a row number, which in your example would simply be 1, 2, 3, 4, 5. Now, take the difference from the date, and you have a constant value for the group.

    select name, date,
           dateadd(d, - row_number() over (partition by name order by date), date) as val
    from t
    

    Finally, you want the number of groups in sequence. I would also add a group identifier (for instance, to distinguish between the last two).

    select name, date,
           count(*) over (partition by name, val) as NumInSeq,
           dense_rank() over (partition by name order by val) as SeqID
    from (select name, date,
                 dateadd(d, - row_number() over (partition by name order by date), date) as val
          from t
         ) t
    

    Somehow, I missed the part about weekdays and holidays. This solution does not solve that problem.

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

Sidebar

Related Questions

I've been trying to write a PHP function which searches the id index valeus
i am trying to write a zoom function which looks something like this: centre
i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
I'm trying to make it so I can write a function which defines a
I'm trying to write a function that takes multiple arguments, which can come either
What I am trying is write recursive function which returns the least common divisor
I am currently learning Haskell. I am trying to write a function which given
Im trying to write a recursive function which returns the factorial of a number.
I am trying to write a function in C++ which takes two 64 bit
I'm trying to write a program with a function which returns a matrix with

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.