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 – Generate the report on Missing Year – Month data

SQL Puzzle: SQL Advance Query – Generate the report on Missing Year – Month data

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 report on missing Year – Month data.

Input Data:

1
2
3
4
5
6
7
8
YearMonthCol
------------
201501
201506
201601
201608
201702
201704

Expected Data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
AllMonths
---------
2017-04
2017-03
2017-02
2017-01
2016-12
2016-11
2016-10
2016-09
2016-08
2016-07
2016-06
2016-05
2016-04
2016-03
2016-02
2016-01
2015-12
2015-11
2015-10
2015-09
2015-08
2015-07
2015-06
2015-05
2015-04
2015-03
2015-02
2015-01

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE TABLE Dates
(YearMonthCol INT)
GO
INSERT Dates
VALUES
('201501')
,('201506')
,('201601')
,('201608')
,('201702')
,('201704')
GO

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;WITH CTE AS
(
SELECT CAST ( LEFT(YearMonthCol,4) + '/' + RIGHT(YearMonthCol,2) + '/' + '01' AS VARCHAR(10)) YearMonthCol ,ROW_NUMBER() OVER ( ORDER BY YearMonthCol )
ranker FROM Dates
)
,CTE1 AS
(
SELECT c1.YearMonthCol, CAST ( '' + CAST(c1.ranker AS VARCHAR(10)) + '' AS XML ) ranker,c1.ranker rnk
,ISNULL(c2.YearMonthCol,c1.YearMonthCol) YearMonthEnds
FROM CTE c1
LEFT JOIN CTE c2
ON c1.ranker = c2.ranker - 1
)
,CTE2 AS
(
SELECT *, DATEDIFF ( M ,YearMonthCol, YearMonthEnds ) diff , CAST ( REPLICATE( CAST ( ranker AS VARCHAR(10)) + ',' ,
CASE WHEN DATEDIFF ( M ,YearMonthCol, YearMonthEnds ) > 1 THEN DATEDIFF ( M ,YearMonthCol, YearMonthEnds ) ELSE 1 END ) AS XML ) xmlcol FROM CTE1 )
SELECT CONVERT(Varchar(7),DATEADD(m,RowNumber,YearMonthCol),121) as AllMonths FROM CTE2 s
CROSS APPLY
(
SELECT project.D.value('.','VARCHAR(50)') as SplitData,ROW_NUMBER() OVER(Partition by Project.D.value('.', 'varchar(50)') ORDER BY (SELECT NULL)) -1 as RowNumber
FROM s.xmlcol.nodes('x') as project(D)
) t
ORDER BY AllMonths DESC

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

Oct 25, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Find the average shared salary by each departmentSQL Puzzle: SQL Advance Query - To Swap the values in the Table
Comments: 3
  1. Dinesh.IS
    October 27, 2017 at 8:21 am

    ;
    WITH CTE
    As
    (Select Convert(DateTime,Convert(VarChar(6),YearMonthCol)+’01’) As DateAndTime From Dates
    )
    ,
    N
    As
    (Select Min(DateAndTime) As FromDate,Max(DateAndTime) As EndDate
    From CTE
    ),
    F
    As
    (Select FromDate,EndDate
    From N
    Union All
    Select DateAdd(M,1,FromDate),EndDate
    From F Where DateAdd(M,1,FromDate)<=EndDate
    )
    Select Left(Convert(VarChar,FromDate,111),7) As AllMonths From F

    ReplyCancel
  2. Dinesh.IS
    October 27, 2017 at 8:22 am

    ;
    WITH CTE
    As
    (Select Convert(DateTime,Convert(VarChar(6),YearMonthCol)+’01’) As DateAndTime From Dates
    ),
    N
    As
    (Select Min(DateAndTime) As FromDate,Max(DateAndTime) As EndDate
    From CTE
    Union All
    Select DateAdd(M,1,FromDate) As FromDate,EndDate
    From N Where DateAdd(M,1,FromDate)<=EndDate)
    Select Left(Convert(VarChar,FromDate,111),7) As AllMonths From N

    ReplyCancel
  3. Sarfaraz Mohammed
    December 23, 2019 at 12:19 pm

    DECLARE @Dates TABLE
    (YearMonthCol INT)

    INSERT @Dates
    VALUES
    (‘201501’)
    ,(‘201506’)
    ,(‘201601’)
    ,(‘201608’)
    ,(‘201702’)
    ,(‘201704′)

    DECLARE @MinDate VARCHAR(8),@MaxDate VARCHAR(8)
    SELECT @MinDate=CONCAT(MIN(d.YearMonthCol),’01’),@MaxDate=CONCAT(MAX(d.YearMonthCol),’01’) FROM @Dates d

    ;WITH CTE_Data
    AS
    (
    SELECT TOP (DATEDIFF(MONTH,@MinDate,@MaxDate)+1)
    CONVERT(VARCHAR(6),DATEADD(MONTH, ROW_NUMBER() OVER(ORDER BY a.object_id) – 1, @MinDate),112) AS AllMonths
    FROM sys.all_objects a
    CROSS JOIN sys.all_objects b
    )
    SELECT AllMonths FROM CTE_Data cd
    ORDER BY 1 DESC

    ReplyCancel

Leave a Reply to Dinesh.IS Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

October 25, 2017 3 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, Month, RANGE, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks, Year
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....