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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:55:19+00:00 2026-06-11T16:55:19+00:00

I m working on a very weird problem with SQL where I have to

  • 0

I m working on a very weird problem with SQL where I have to compare previous rows

Number    start_date                      end_date    
 -----     -------                      ------------
 1    2011-06-07 00:00:00.000         2011-07-10 00:00:00.000   
 2    2011-10-11 00:00:00.000         2011-10-11 00:00:00.000   
 3    2011-10-26 00:00:00.000         2011-10-29 00:00:00.000   
 4    2011-10-29 00:00:00.000         2011-11-15 00:00:00.000   

Here , I have to compare the start_date and end_date on the two different line and create a view out of it.
(If the start_date is less than the previous end_date , then criteria is set to 1).

Well it should compare 2011-10-26 00:00:00.000 for 3 and 2011-10-27 00:00:00.000 on 2 for 30 days

Number    start_date                      end_date                 Criteria
-----     -----------                   ----------------          ------------
1   2011-06-07 00:00:00.000       2011-07-10 00:00:00.000            0
2   2011-10-11 00:00:00.000       2011-10-11 00:00:00.000            0
3   2011-10-26 00:00:00.000       2011-10-29 00:00:00.000            1
4   2011-10-30 00:00:00.000       2011-11-15 00:00:00.000            1   

I m confused how should I proceed with this.

Any help would be helpful !!!!

Thanks !!!

  • 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-11T16:55:20+00:00Added an answer on June 11, 2026 at 4:55 pm

    The most straightforward way to do this is to use a subquery:

    select A.number, a.start_date, a.end_date,
        CASE WHEN start_date < dateadd(d,30,(select TOP(1) b.end_date
         from mytable B
         where B.number < A.number
         order by B.number desc)) then 1 else 0 end Criteria
    from mytable A
    

    Note: If the start date is the 29th day following the previous row’s end date, Criteria becomes 1. By the 30th day onwards, it is 0. Tweak the 30 in the query as required.

    Sample:

    create table mytable (
        Number int primary key,
        start_date datetime,
        end_date datetime);
    
    insert mytable
    select 1, '2011-06-07', '2011-07-10' union all
    select 2, '2011-10-11', '2011-10-27' union all
    select 3, '2011-10-26', '2011-10-29' union all
    select 4, '2011-10-29', '2011-11-15'
    

    Result:

    number  start_date  end_date    Criteria
    1   2011-06-07 00:00:00.000 2011-07-10 00:00:00.000 0
    2   2011-10-11 00:00:00.000 2011-10-27 00:00:00.000 0
    3   2011-10-26 00:00:00.000 2011-10-29 00:00:00.000 1
    4   2011-10-29 00:00:00.000 2011-11-15 00:00:00.000 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very weird problem with resx based localization. At first, let me
We have a very odd problem here with SVN. Let me first explain the
I'm having a very weird problem.I'm working on an assignment that involves building a
I'm working with jQuery's .get() function and I'm having a very weird problem using
I am having a very weird problem with building a SQL Server 2008 Database
Ok, very very weird issue here. I have a sub nav menu that links
I have developed a simple location aware iPhone application which is functionally working very
I'm working with a very small sample project that can be downloaded from here
I am experiencing some weird behavior with an android app I have been working
Very weird problem, some RowKeys looks like they are getting 'locked' after some time.

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.