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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:40:22+00:00 2026-06-10T01:40:22+00:00

I have been struggling with a problem that should be pretty simple actually but

  • 0

I have been struggling with a problem that should be pretty simple actually but after a full week of reading, googling, experimenting and so on, my colleague and we cannot find the proper solution. 🙁

The problem: We have a table with two values:
an employeenumber (P_ID, int) <— identification of employee
a date (starttime, datetime) <— time employee checked in

  • We need to know what periods each employee has been working.
  • When two dates are less then @gap days apart, they belong to the same period
  • For each employee there can be multiple records for any given day but I just need to know which dates he worked, I am not interested in the time part
  • As soon as there is a gap > @gap days, the next date is considered the start of a new range
  • A range is at least 1 day (example: 21-9-2011 | 21-09-2011) but has no maximum length. (An employee checking in every @gap – 1 days should result in a period from the first day he checked in until today)

What we think we need are the islands in this table where the gap in days is greater than @variable (@gap = 30 means 30 days)

So an example:

SOURCETABLE:

P_ID  | starttime
------|------------------
12121 | 24-03-2009 7:30
12121 | 24-03-2009 14:25 
12345 | 27-06-2011 10:00
99999 | 01-05-2012 4:50 
12345 | 27-06-2011 10:30
12345 | 28-06-2011 11:00
98765 | 13-04-2012 10:00
12345 | 21-07-2011 9:00
99999 | 03-05-2012 23:15
12345 | 21-09-2011 12:00
45454 | 12-07-2010 8:00
12345 | 21-09-2011 17:00
99999 | 06-05-2012 11:05
99999 | 20-05-2012 12:45
98765 | 26-04-2012 16:00
12345 | 07-07-2012 14:00
99999 | 01-06-2012 13:55
12345 | 13-08-2012 13:00

Now what I need as a result is:

PERIODS:

P_ID  |   Start    |    End
-------------------------------
12121 | 24-03-2009 | 24-03-2009
12345 | 27-06-2012 | 21-07-2012
12345 | 21-09-2012 | 21-09-2012
12345 | 07-07-2012 | (today) OR 13-08-2012  <-- (less than @gap days ago) OR (last date in table)
45454 | 12-07-2010 | 12-07-2010
45454 | 17-06-2012 | 17-06-2012 
98765 | 13-04-2012 | 26-04-2012
99999 | 01-05-2012 | 01-06-2012

I hope this is clear this way, I already thank you for reading this far, it would be great if you could contribute!

  • 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-10T01:40:24+00:00Added an answer on June 10, 2026 at 1:40 am

    Jon most definitively has shown us the right direction. Performance was horrible though (4million+ records in the database). And it looked like we were missing some information. With all that we learned from you we came up with the solution below. It uses elements of all the proposed answers and cycles through 3 temptables before finally spewing results but performance is good enough, as well as the data it generates.

    declare @gap int
    declare @Employee_id int
    
    set @gap = 30   
    set dateformat dmy
    --------------------------------------------------------------- #temp1 --------------------------------------------------
    CREATE TABLE #temp1 ( EmployeeID int, starttime date)
    INSERT INTO #temp1 ( EmployeeID, starttime)
    
    select distinct ck.Employee_id, 
                    cast(ck.starttime as date)
    from SERVER1.DB1.dbo.checkins pd
            inner join SERVER1.DB1.dbo.Team t on ck.team_id = t.id
    where t.productive = 1
    
    --------------------------------------------------------------- #temp2 --------------------------------------------------
    
    create table #temp2 (ROWNR int, Employeeid int, ENDOFCHECKIN datetime, FIRSTCHECKIN datetime)
    INSERT INTO #temp2 
    
    select Row_number() OVER (partition by EmployeeID ORDER BY t.prev) + 1 as ROWNR,
                 EmployeeID,
                 DATEADD(DAY, 1, t.Prev) AS start_gap,
               DATEADD(DAY, 0, t.next) AS end_gap
    from 
                 (
                        select a.EmployeeID,
                                      a.starttime as Prev, 
                                      (
                                      select min(b.starttime)
                                      from #temp1 as b
                                      where starttime > a.starttime and b.EmployeeID = a.EmployeeID 
                                      ) as Next
    from #temp1 as a) as t
    
    where  datediff(day, prev, next ) > 30
    group by     EmployeeID,
                        t.Prev,
                        t.next
    union -- add first known date for Employee 
    
    select      1 as ROWNR,
                EmployeeID,
                NULL,
                min(starttime)
    from #temp1 ct
    group by ct.EmployeeID
    
    --------------------------------------------------------------- #temp3 --------------------------------------------------
    
    create table #temp3 (ROWNR int, Employeeid int, ENDOFCHECKIN datetime, STARTOFCHECKIN datetime)
    INSERT INTO #temp3
    
    select  ROWNR,
            Employeeid,
            ENDOFCHECKIN,
            FIRSTCHECKIN
    from #temp2 
    
    union -- add last known date for Employee 
    
    select       (select count(*) from #temp2 b where Employeeid = ct.Employeeid)+1 as ROWNR,
                 ct.Employeeid,
                (select dateadd(d,1,max(starttime)) from #temp1 c where Employeeid = ct.Employeeid),
                 NULL
    from #temp2 ct
    group by ct.EmployeeID
    
    ---------------------------------------finally check our data-------------------------------------------------
    
    
    select              a1.Employeeid,
                        a1.STARTOFCHECKIN as STARTOFCHECKIN,
                        ENDOFCHECKIN = CASE WHEN b1.ENDOFCHECKIN <= a1.STARTOFCHECKIN THEN a1.ENDOFCHECKIN ELSE b1.ENDOFCHECKIN END,
                        year(a1.STARTOFCHECKIN) as JaarSTARTOFCHECKIN,
                        JaarENDOFCHECKIN = CASE WHEN b1.ENDOFCHECKIN <= a1.STARTOFCHECKIN THEN  year(a1.ENDOFCHECKIN) ELSE  year(b1.ENDOFCHECKIN) END,
                        Month(a1.STARTOFCHECKIN) as MaandSTARTOFCHECKIN,
                        MaandENDOFCHECKIN = CASE WHEN b1.ENDOFCHECKIN <= a1.STARTOFCHECKIN THEN  month(a1.ENDOFCHECKIN) ELSE  month(b1.ENDOFCHECKIN) END,
                        (year(a1.STARTOFCHECKIN)*100)+month(a1.STARTOFCHECKIN) as JaarMaandSTARTOFCHECKIN,
                        JaarMaandENDOFCHECKIN = CASE WHEN b1.ENDOFCHECKIN <= a1.STARTOFCHECKIN THEN (year(a1.ENDOFCHECKIN)*100)+month(a1.STARTOFCHECKIN) ELSE (year(b1.ENDOFCHECKIN)*100)+month(b1.ENDOFCHECKIN) END,
                        datediff(M,a1.STARTOFCHECKIN,b1.ENDOFCHECKIN) as MONTHSCHECKEDIN
    from #temp3 a1
           full outer join #temp3 b1 on a1.ROWNR = b1.ROWNR -1 and a1.Employeeid = b1.Employeeid
    where not (a1.STARTOFCHECKIN is null AND b1.ENDOFCHECKIN is null) 
    order by a1.Employeeid, a1.STARTOFCHECKIN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realsie that similar questions have been answered here about this problem but im
I have been struggling with a problem that at first glance might look easy
I have been struggling to find a solution to this problem. In my code,
I've been struggling with Zend_Navigation all weekend, and now I have another problem, which
I have been struggling all afternoon trying to get a simple mailto: tag to
I have been struggling with this seeminly easy problem for 48 hours, and I
I have been struggling with a problem for a while and so far have
I've been struggling with how I should indicate that a certain record in a
I've been struggling with an array and its increments, but I think I have
I have been struggling for too long a time now with a rather simple

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.