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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:29:08+00:00 2026-05-27T05:29:08+00:00

I have the following table schema: RecordId EmpID AbsCode DateFrom DateTo ————————————————————— 666542 1511

  • 0

I have the following table schema:

RecordId    EmpID       AbsCode DateFrom    DateTo
---------------------------------------------------------------
666542      1511        AB      09/11/2011  10/11/2011
666986      1511        AB      11/11/2011  11/11/2011
666996      1511        EL      13/11/2011  17/11/2011
755485      1787        SL      01/11/2011  14/11/2011
758545      1787        SL      15/11/2011  26/11/2011
796956      1954        AB      09/11/2011  09/11/2011
799656      1367        AB      09/11/2011  09/11/2011
808845      1527        EL      16/11/2011  16/11/2011
823323      1527        EL      17/11/2011  17/11/2011
823669      1527        EL      18/11/2011  18/11/2011
899555      1123        AB      09/11/2011  09/11/2011
990990      1511        AB      12/11/2011  12/11/2011

As you can see, the data is entered separately for the same employee. Let’s say he reported SL (Sick Leave) for one day, the data is entered, Then he called next day to report another sick leave for two days..etc. Now what I want is to merge these entries when presenting it to the client so that all consecutive Absences with the same absence code will be merged. For example the table above should look exactly like this:

EmpID       AbsCode DateFrom    DateTo
-------------------------------------------------------------
1511        AB      09/11/2011  12/11/2011
1511        EL      13/11/2011  17/11/2011
1787        SL      01/11/2011  26/11/2011
1954        AB      09/11/2011  09/11/2011
1367        AB      09/11/2011  09/11/2011
1527        EL      16/11/2011  18/11/2011
1123        AB      09/11/2011  09/11/2011

I am not an SQL guy, I can do it using a loop in C# to iterate a DataSet or DataReader but I hope to do this with T-SQL in a stored proc. I have found similar questions in StackOverFlow and checked them all, None of them applies for the above sample table.

EDIT:
Sometimes I will Have a situations like this:

RecordId    EmpID       AbsCode DateFrom    DateTo
---------------------------------------------------------------
666542      1511        AB      09/11/2011  10/11/2011
666986      1511        AB      11/11/2011  25/12/2011

As you can see, this Employee had Absence from 9/11/2011 (d/M/yyyy) to (25/12/2011) but the client requested to have Absence list from 1st of December until 31st of December, So the result should be:

EmpID       AbsCode DateFrom    DateTo
-------------------------------------------------------------
1511        AB      01/12/2011  12/11/2011

So basically, It will show the result according to parameters supplied (from, to). If the record stared before the requested period, It will show it but at the same time it will show the start of the record according to the parameters supplied, Same applies from records endings after the (from, to) parameters.

  • 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-27T05:29:08+00:00Added an answer on May 27, 2026 at 5:29 am

    This is a CTE so it’ll all need to be executed as one, but I’ll explain as I go.

    First I’ll set the parameters for the date range we are interested in:

    DECLARE @StartDate DateTime; SET @StartDate = '2011-11-01';  
    DECLARE @EndDate DateTime; SET @EndDate = '2011-11-30';  
    

    Then I’ll turn them into a list of dates using a recursive CTE

    WITH 
        ValidDates ( ValidDate ) AS 
            (
                SELECT @StartDate 
                    UNION ALL
                SELECT DateAdd(day, 1, ValidDate) 
                    FROM ValidDates 
                    WHERE ValidDate < @EndDate
            ),
    

    By joining that with ranges in the original records I get a list of individual days absence.

    Using a combination of row_number and datediff I can group consecutive dates. This assumes that there are no duplicates.

        DaysAbsent AS 
            (
                SELECT 
                      A.RecordID
                    , A.EmpID
                    , A.AbsCode
                    , DateDiff(Day, @StartDate, D.ValidDate) 
                        - row_number() 
                            over (partition by A.EmpID, A.AbsCode  
                                order by D.ValidDate) AS DayGroup
                    , D.ValidDate AS AbsentDay
                FROM 
                    dbo.Absence A
                        INNER JOIN  
                    ValidDates D
                        ON D.ValidDate >= DateFrom 
                           and  D.ValidDate <= DateTo 
            )
    

    Now it’s a simple select with min and max to turn it back into ranges.

    SELECT 
          EmpID
        , AbsCode
        , MIN(AbsentDay) AS DateFrom
        , MAX(AbsentDay) AS DateTo
    FROM
        DaysAbsent
    GROUP BY
          EmpID
        , AbsCode
        , DayGroup
    

    The DayGroup isn’t needed in the output but is needed for the grouping, otherwise non consecutive groups will be collapsed into one.

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

Sidebar

Related Questions

I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT
I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I have no control over database schema and have the following (simplified) table structure:
In a table I have the following schema table1: playerID int primary key, nationalities
Assuming I have the following schema table A: ID int primary key value varchar(255)
I have test_scores table with following fields: Table schema: id (number) score1 (number) score2
I have a table schema similar to the following (simplified): CREATE TABLE Transactions (
I have the following schema: TABLE bands -> band_id -> property1 -> property2 ->
I have the following database schema: table courses: id tutor_id title table course_categories: id
I have the following schema (generated from an existing table with the schema module

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.