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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:37:58+00:00 2026-06-17T07:37:58+00:00

I have a dataset (DATASET1) that lists all employees with their Dept IDs, the

  • 0

I have a dataset (DATASET1) that lists all employees with their Dept IDs, the date they started and the date they were terminated.

I’d like my query to return a dataset in which every row represents a day for each employee stayed employed, with number of days worked (Start-to-Date).

How do I this query? Thanks for your help, in advance.

DATASET1

DeptID     EmployeeID   StartDate   EndDate
--------------------------------------------
001        123           20100101   20120101   
001        124           20100505   20130101

DATASET2

DeptID     EmployeeID    Date       #ofDaysWorked
--------------------------------------------
001        123           20100101   1
001        123           20100102   2
001        123           20100103   3
001        123           20100104   4
....       ....          ........   ...

EIDT: My goal is to build a fact table which would be used to derive measures in SSAS. The measure I am building is ‘average length of employment’. The measure will be deployed in a dashboard and the users will have the ability to select a calendar period and drill-down into month, week and days. That’s why I need to start with such a large dataset. Maybe I can accomplish this goal by using MDX queries but how?

  • 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-17T07:37:59+00:00Added an answer on June 17, 2026 at 7:37 am

    You can use a recursive CTE to perform this:

    ;with data (deptid, employeeid, inc_date, enddate) as
    (
      select deptid, employeeid, startdate, enddate
      from yourtable
      union all
      select deptid, employeeid,
        dateadd(d, 1, inc_date),
        enddate
      from data
      where dateadd(d, 1, inc_date) <= enddate
    ) 
    select deptid,
      employeeid,
      inc_date,
      rn NoOfDaysWorked
    from
    (
      select deptid, employeeid,
        inc_date, 
        row_number() over(partition by deptid, employeeid
                          order by inc_date) rn
      from data
    ) src
    OPTION(MAXRECURSION 0)
    

    See SQL Fiddle with Demo

    The result is similar to this:

    | DEPTID | EMPLOYEEID |       DATE | NOOFDAYSWORKED |
    -----------------------------------------------------
    |      1 |        123 | 2010-01-01 |              1 |
    |      1 |        123 | 2010-01-02 |              2 |
    |      1 |        123 | 2010-01-03 |              3 |
    |      1 |        123 | 2010-01-04 |              4 |
    |      1 |        123 | 2010-01-05 |              5 |
    |      1 |        123 | 2010-01-06 |              6 |
    |      1 |        123 | 2010-01-07 |              7 |
    |      1 |        123 | 2010-01-08 |              8 |
    |      1 |        123 | 2010-01-09 |              9 |
    |      1 |        123 | 2010-01-10 |             10 |
    |      1 |        123 | 2010-01-11 |             11 |
    |      1 |        123 | 2010-01-12 |             12 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataset that i am trying to set the default value as
I have a dataset that returns questions and answers from the database, each answer
I have a large dataset that load in with a fill method on page
I have a large dataset that I am dealing with in Python. It is
I have a (dense) dataset that consist of 5 groups, so my data.frame looks
Imagine you have a large dataset that may or may not be filtered by
I have a DataSet with some DataTables that are linked together with DataRelations (classic
I have a dataset of 1 minute data of 1000 stocks since 1998, that
I have a datagrid that is filled with a dataset from an SQL query.
I have a UltraWinGrid that is bound to a DataSet, in which a couple

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.