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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:03:21+00:00 2026-05-20T10:03:21+00:00

Being primarily a C# developer, I’m scratching my head when trying to create a

  • 0

Being primarily a C# developer, I’m scratching my head when trying to create a pure T-SQL based solution to a problem involving summarizing days/month given a set of date ranges.

I have a set of data looking something like this:

UserID  Department  StartDate   EndDate
======  ==========  ==========  ==========
1       A           2011-01-02  2011-01-05
1       A           2011-01-20  2011-01-25
1       A           2011-02-25  2011-03-05
1       B           2011-01-21  2011-01-22
2       A           2011-01-01  2011-01-20
3       C           2011-01-01  2011-02-03

The date ranges are non-overlapping, may span several months, there may exist several ranges for a specific user and department within a single month.
What I would like to do is to summarize the number of days (inclusive) per user, department, year and month, like this (with reservations for any math errors in my example…):

UserID  Department  Year  Month  Days
======  ==========  ====  =====  ====
1       A           2011  01     10
1       A           2011  02     4
1       A           2011  03     5
1       B           2011  01     2
2       A           2011  01     20
3       C           2011  01     31
3       C           2011  02     3

This data is going into a new table used by reporting tools.
I hope the problem description is clear enough, this is my first posting here, be gentle 🙂

Thanks in advance!

  • 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-20T10:03:21+00:00Added an answer on May 20, 2026 at 10:03 am

    Working sample

    -- sample data in a temp table
    declare @t table (UserID int, Department char(1), StartDate datetime, EndDate datetime)
    insert @t select
    1 ,'A', '2011-01-02','2011-01-05'union all select
    1 ,'A', '2011-01-20','2011-01-25'union all select
    1 ,'A', '2011-02-25','2011-03-05'union all select
    1 ,'B', '2011-01-21','2011-01-22'union all select
    2 ,'A', '2011-01-01','2011-01-20'union all select
    3 ,'C', '2011-01-01','2011-02-03'
    
    -- the query you need is below this line    
    
    select UserID, Department,
        YEAR(StartDate+v.number) Year,
        MONTH(StartDate+v.number) Month, COUNT(*) Days
    from @t t
    inner join master..spt_values v
      on v.type='P' and v.number <= DATEDIFF(d, startdate, enddate)
    group by UserID, Department, YEAR(StartDate+v.number), MONTH(StartDate+v.number)
    order by UserID, Department, Year, Month
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in
Despite primarily being a windows user, I am a huge fan of rsync. Now,
Being a C# developer since version 1.0, F# has captured my free time for
Being new to test based development, this question has been bugging me. How much
Being a application developer, do I need to know Unicode?
I'm trying to find a good work around to not being able to use
Problem: When adding list item the Value field is being set to the Text
I am having a similar issue to this person . The primary difference being
Being relatively new to the .net game, I was wondering, has anyone had any
Being stuck with a legacy database schema that no longer reflects your data model

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.