Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 October SQL Puzzle: SQL Advance Query – Solve Date Overlapping Problem

SQL Puzzle: SQL Advance Query – Solve Date Overlapping Problem

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output to find the date record where EndDate +1 = Start date of next record for removing the overlapping issue.

Input Data:

1
2
3
4
5
6
7
Id StartDate EndDate
----------- ---------- ----------
1 2017-01-20 2017-02-26
2 2017-03-08 2017-05-16
3 2017-08-24 2017-09-30
4 2017-10-01 2017-11-26
5 2017-11-28 2017-12-30

Expected Output:

1
2
3
StartDate EndDate
---------- ----------
2017-08-24 2017-10-01

Create a table with data:

1
2
3
StartDate EndDate
---------- ----------
2017-08-24 2017-10-01

Solution 1:

1
2
3
select StartDate, (select StartDate from Dates where Id = a.Id+1) as EndDate
from Dates a
where 1 in (select DATEDIFF(DAY,a.EndDate,b.StartDate) from Dates b )

Solution 2:

1
2
3
4
5
6
7
8
9
10
;WITH CTE AS
(
SELECT a.*,DATEADD(DAY, 1, A.EndDate) as EDate
FROM Dates a LEFT OUTER JOIN Dates b
ON a.Id = b.Id
)
SELECT b.StartDate , b.EDate AS EndDate
FROM CTE c
LEFT JOIN CTE b ON c.StartDate = b.EDate
WHERE b.Id IS NOT NULL

Please try the different solution for this puzzle and share it via comment...

Oct 10, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Partition Employee Table in Three partsSQL Puzzle: SQL Advance Query - Find number the first day of Quarter of Year
Comments: 2
  1. Dinesh.IS
    October 11, 2017 at 7:39 am

    Select A.StartDate,NextStartDate As EndDate
    From (Select DateAdd(Day,1,EndDate) As NextStartDate,StartDate,EndDate From Dates) A
    Inner JOin Dates B On B.StartDate=A.NextStartDate

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      October 11, 2017 at 9:20 am

      thanks for answers and solutions, you are sharing for almost all puzzles… Appreciate your work.

      ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

October 10, 2017 2 Comments SQL PuzzleAnvesh Patel, database, database research and development, Date, dbrnd, Overlapping, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....