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

The Archive Base Latest Questions

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

How do I get the desired result in T-SQL like …. like I have

  • 0

How do I get the desired result in T-SQL like ….
like
I have a Record like

UseriD    InDate    outDate
1         3/12/2010   3/12/2010
1         3/12/2010   3/13/2010
1         3/19/2010   3/30/2010   
2         3/2/2010    3/3/2010  
2         3/3/2010    3/4/2010
2         3/4/2010    3/29/2010 
3         2/2/2010    2/28/2010

so our result must be like this

UseriD    InDate    outDate
1         3/12/2010   3/13/2010
1         3/19/2010   3/30/2010   
2         3/2/2010    3/29/2010 
3         2/2/2010    2/28/2010

How can we do this is T-Sql

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

    I would like to share my solution to help anyone who comes accross the same problem:

    /****** Object:  StoredProcedure [dbo].[MergeSeqDates]    Script Date: 06/12/2010 18:18:26 ******/
    /************************************************************************************************
    THIS STORED PROCEDURE CAN BE USED FOR AN INPUT TABLE LIKE THE FOLLOWING 
    tablename: [_dateSeq]
    UserId  InDate                      OutDate id          Record number
    1       3/12/2010 12:00:00 AM   3/12/2010 12:00:00 AM   1 
    1       3/12/2010 12:00:00 AM   3/13/2010 12:00:00 AM   2 
    1       3/19/2010 12:00:00 AM   3/30/2010 12:00:00 AM   3 
    2       3/2/2010 12:00:00 AM    3/3/2010 12:00:00 AM    4 
    2       3/3/2010 12:00:00 AM    3/4/2010 12:00:00 AM    5 
    2       3/4/2010 12:00:00 AM    3/9/2010 12:00:00 AM    6 
    3       2/2/2010 12:00:00 AM    2/28/2010 12:00:00 AM   7 
    
    
    TO GIVE AN OUTPUT TABLE LIKE:
    tablename: [mergeddateseq]
    UserId  InDate                  OutDate                 Unique_Id 
    1       3/12/2010 12:00:00 AM   3/13/2010 12:00:00 AM   1
    1       3/19/2010 12:00:00 AM   3/30/2010 12:00:00 AM   2
    2       3/2/2010 12:00:00 AM    3/9/2010 12:00:00 AM    3
    3       2/2/2010 12:00:00 AM    2/28/2010 12:00:00 AM   4
    
    
    Table Structures used:
    (1)
    CREATE TABLE [dbo].[_dateSeq](
        [UserId] [bigint] NULL,
        [InDate] [datetime] NULL,
        [OutDate] [datetime] NULL,
        [id] [bigint] IDENTITY(1,1) NOT NULL,
     CONSTRAINT [PK__dateSeq] PRIMARY KEY CLUSTERED 
    (
        [id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    
    
    (2)
    CREATE TABLE [dbo].[MergedDateSeq](
        [Unique_Id] [bigint] IDENTITY(1,1) NOT NULL,
        [UserId] [bigint] NULL,
        [InDate] [datetime] NULL,
        [OutDate] [datetime] NULL,
     CONSTRAINT [PK_MergedDateSeq] PRIMARY KEY CLUSTERED 
    (
        [Unique_Id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    
    ************************************************************************************************/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    
    ALTER Procedure [dbo].[MergeSeqDates]
    (
    @StartDate datetime,
    @EndDate datetime
    )
    AS
    BEGIN
    /*Clear the output table before new data is put into it*/
        DROP TABLE mergeddateseq;
        CREATE TABLE [dbo].[MergedDateSeq](
        [Unique_Id] [bigint] IDENTITY(1,1) NOT NULL,
        [UserId] [bigint] NULL,
        [InDate] [datetime] NULL,
        [OutDate] [datetime] NULL,
     CONSTRAINT [PK_MergedDateSeq] PRIMARY KEY CLUSTERED 
    (
        [Unique_Id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    /*For every record in the original table, check if the user's next start date is consequent to the user's previous end date*/
    /*If yes, update the earlier record*/
    /*If not, add a new record record*/
    
    DECLARE rec_cursor CURSOR FOR
    SELECT UserId,InDate,OutDate FROM [_dateSeq] WHERE InDate>=@StartDate and ((OutDate<=@EndDate) or (OutDate is null)) order by InDate;
    
    
    OPEN rec_cursor
    
    Declare @DateFrom DateTime
    Declare @DateTo DateTime
    Declare @CardId bigint
    
    Declare @mrgDateFrom DateTime
    Declare @mrgDateTo DateTime
    Declare @mrgCardId bigint
    Declare @Unque_Id bigint
    
    FETCH NEXT FROM rec_cursor
    INTO @CardId,@DateFrom, @DateTo
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
        set @Unque_Id=0;
        SELECT @Unque_Id=Unique_Id,@mrgCardId=UserId,@mrgDateFrom=InDate,@mrgDateTo=OutDate FROM mergeddateseq where UserId=@CardId order by OutDate desc;
        if @@rowcount>0
            BEGIN
            --check dates
            --update record if date is same
            if (@DateFrom=@mrgDateTo)
                Update mergeddateseq set OutDate=@DateTo where Unique_Id=@Unque_Id;
            --update record if dates are sequential
            else if (@DateFrom=DATEADD(day,+1,@mrgDateTo))
                Update mergeddateseq set OutDate=@DateTo where Unique_Id=@Unque_Id;
            else
            --insert new record
            Insert into mergeddateseq (UserId,InDate,OutDate) values (@CardId,@DateFrom,@DateTo);
            END
        else
            BEGIN
            --insert new record
            Insert into mergeddateseq (UserId,InDate,OutDate) values (@CardId,@DateFrom,@DateTo);
            END
    
        FETCH NEXT FROM rec_cursor
        INTO @CardId,@DateFrom, @DateTo
    END
    
    CLOSE rec_cursor
    DEALLOCATE rec_cursor
    
        Select * from mergeddateseq;
    END
    
    --exec [MergeSeqDates] @StartDate='1-1-2010', @EndDate='1-1-2011'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.