<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Year &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/year/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Fri, 06 Oct 2017 18:59:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Generate the report on Missing Year &#8211; Month data</title>
		<link>https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-generate-the-report-on-missing-year-month-data/</link>
					<comments>https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-generate-the-report-on-missing-year-month-data/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 25 Oct 2017 16:16:18 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Month]]></category>
		<category><![CDATA[RANGE]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[Year]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5937</guid>

					<description><![CDATA[Check the below input data and expected output to generate the report on missing Year &#8211; Month data. Input Data: [crayon-6ab55af953ad8179325878/] Expected Data: [crayon-6ab55af953adb144574342/] Create a table with sample data: [crayon-6ab55af953adc933580896/] Solution: [crayon-6ab55af953add724002113/] Please try the different solution for this puzzle and share it via comment...]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F10%2Fsql-puzzle-sql-advance-query-generate-the-report-on-missing-year-month-data%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20report%20on%20Missing%20Year%20%E2%80%93%20Month%20data" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F10%2Fsql-puzzle-sql-advance-query-generate-the-report-on-missing-year-month-data%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20report%20on%20Missing%20Year%20%E2%80%93%20Month%20data" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F10%2Fsql-puzzle-sql-advance-query-generate-the-report-on-missing-year-month-data%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20report%20on%20Missing%20Year%20%E2%80%93%20Month%20data" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F10%2Fsql-puzzle-sql-advance-query-generate-the-report-on-missing-year-month-data%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20report%20on%20Missing%20Year%20%E2%80%93%20Month%20data" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F10%2Fsql-puzzle-sql-advance-query-generate-the-report-on-missing-year-month-data%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20report%20on%20Missing%20Year%20%E2%80%93%20Month%20data" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F10%2Fsql-puzzle-sql-advance-query-generate-the-report-on-missing-year-month-data%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20report%20on%20Missing%20Year%20%E2%80%93%20Month%20data" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F10%2Fsql-puzzle-sql-advance-query-generate-the-report-on-missing-year-month-data%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20report%20on%20Missing%20Year%20%E2%80%93%20Month%20data" data-a2a-url="https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-generate-the-report-on-missing-year-month-data/" data-a2a-title="SQL Puzzle: SQL Advance Query – Generate the report on Missing Year – Month data"></a></p><p>Check the below input data and expected output to generate the report on missing Year &#8211; Month data.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">YearMonthCol
------------
201501
201506
201601
201608
201702
201704</pre> </p>
<p><strong>Expected Data:</strong></p>
<pre class="crayon-plain-tag">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</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE Dates 
(YearMonthCol INT)
GO
 
INSERT Dates 
VALUES
('201501')
,('201506')
,('201601')
,('201608')
,('201702')
,('201704')
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;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 ( '&lt;x&gt;' + CAST(c1.ranker AS VARCHAR(10)) + '&lt;/x&gt;' 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 ) &gt; 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</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-generate-the-report-on-missing-year-month-data/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
