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 September SQL Puzzle: SQL Advance Query – Replace Previous Date if difference is only for One Day

SQL Puzzle: SQL Advance Query – Replace Previous Date if difference is only for One Day

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

Check the below input data and expected output to generate the new EndBillDate if only one day difference between two bills then replace the previous date with new bill date.

Input Data:

1
2
3
4
5
6
7
8
9
10
11
12
Id BillCode EndBilllDate
----------- -------- ------------
1 A001 2016-01-01
2 A002 2016-01-08
3 A003 2016-01-16
4 A004 2016-01-17
5 A005 2016-02-08
6 A006 2016-02-15
7 A007 2016-02-16
8 A008 2016-03-08
9 A009 2016-03-15
10 A010 2016-03-16

Expected output – Generate the New EndBillDate:

1
2
3
4
5
6
7
8
9
10
11
12
Id BillCode EndBilllDate NewEndBillDate
----------- -------- ------------ --------------
1 A001 2016-01-01 2016-01-01
2 A002 2016-01-08 2016-01-08
3 A003 2016-01-16 2016-01-17
4 A004 2016-01-17 2016-01-17
5 A005 2016-02-08 2016-02-08
6 A006 2016-02-15 2016-02-16
7 A007 2016-02-16 2016-02-16
8 A008 2016-03-08 2016-03-08
9 A009 2016-03-15 2016-03-16
10 A010 2016-03-16 2016-03-16

Create a table with data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CREATE TABLE BillDates
(
Id INT
,BillCode VARCHAR(5)
,EndBilllDate DATE
)
GO
 
INSERT INTO BillDates
VALUES
(1,'A001','2016-01-01')
,(2,'A002','2016-01-08')
,(3,'A003','2016-01-16')
,(4,'A004','2016-01-17')
,(5,'A005','2016-02-08')
,(6,'A006','2016-02-15')
,(7,'A007','2016-02-16')
,(8,'A008','2016-03-08')
,(9,'A009','2016-03-15')
,(10,'A010','2016-03-16')
GO

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;WITH CTE1 AS
(
SELECT
Id
,BillCode
,EndBilllDate
,DATEDIFF(DAY, (SELECT MIN(EndBilllDate) FROM BillDates),EndBilllDate) - ROW_NUMBER() OVER( ORDER BY EndBilllDate) rnk
FROM BillDates
)
,CTE2 AS
(
SELECT MIN(Id) Id , MIN(BillCode) BillCode , MIN(EndBilllDate) StartDate , MAX (EndBilllDate) EndBilllDate
FROM CTE1
GROUP BY rnk
)
SELECT b.Id , b.BillCode , b.EndBilllDate , a.EndBilllDate NewEndBillDate
FROM CTE1 b
LEFT JOIN CTE2 a
ON b.EndBilllDate BETWEEN a.StartDate AND a.EndBilllDate

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

Sep 7, 2017Anvesh Patel
SQL Server Interview: List out the Best Practices, after installation of SQL ServerGreenplum: Important Parameters of Resource Queue (Workload Management)
Comments: 3
  1. Dinesh.IS
    October 12, 2017 at 7:53 am

    —Method-1
    ;
    WITH CTE
    As
    (Select *,(Select Top 1 Max(EndBilllDate) From BillDates B Where B.EndBilllDate>A.EndBilllDate Group By EndBilllDate Order By EndBilllDate ASC) As NextBillDate
    From BillDates A )
    Select ID,BillCode,EndBilllDate,(Case When DateDiff(Day,EndBilllDate,NextBillDate)=1 Then NextBillDate Else EndBilllDate End) As NextBillDate
    From CTE

    ReplyCancel
  2. Dinesh.IS
    October 12, 2017 at 7:53 am

    —Above 2012 SQL Server Version
    ;
    WITH CTE
    As
    (Select B.*,Lead(EndBilllDate)Over(Order By EndBilllDate) As NextEndBillDate From BillDates B )
    Select ID,BillCode,EndBilllDate,(Case When DateDiff(Day,EndBilllDate,NextEndBillDate)=1 Then NextEndBillDate Else EndBillldate End) As NextEndBillDate
    From CTE

    ReplyCancel
  3. Ram
    August 17, 2018 at 7:19 pm

    select B1.Id,
    B1.BillCode,
    B1.EndBilllDate,
    case when B2.EndBilllDate=DAteAdd(day,1,B1.EndBilllDate)
    Then DateADD(day,1,B1.EndBilllDate)
    else B1.EndBilllDate
    end NewEndBilllDate
    From BillDates b1 left outer join BillDates b2
    on b1.Id +1 = B2.ID

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

September 7, 2017 3 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, 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....