<?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>Missing &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/missing/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>Tue, 21 Nov 2017 05:42:43 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Fill the gap of Missing Codes</title>
		<link>https://www.dbrnd.com/2017/11/sql-puzzle-sql-advance-query-fill-the-gap-of-missing-codes/</link>
					<comments>https://www.dbrnd.com/2017/11/sql-puzzle-sql-advance-query-fill-the-gap-of-missing-codes/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 11 Nov 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[Missing]]></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>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5855</guid>

					<description><![CDATA[Check the below input data and expected output to fill the gap of missing codes. Input Data: [crayon-6aa4ac984ab6b747082592/] Expected Output: [crayon-6aa4ac984ab6e321986881/] Create a table with sample data: [crayon-6aa4ac984ab6f965737195/] Solution: [crayon-6aa4ac984ab70842217195/] 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%2F11%2Fsql-puzzle-sql-advance-query-fill-the-gap-of-missing-codes%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Fill%20the%20gap%20of%20Missing%20Codes" 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%2F11%2Fsql-puzzle-sql-advance-query-fill-the-gap-of-missing-codes%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Fill%20the%20gap%20of%20Missing%20Codes" 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%2F11%2Fsql-puzzle-sql-advance-query-fill-the-gap-of-missing-codes%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Fill%20the%20gap%20of%20Missing%20Codes" 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%2F11%2Fsql-puzzle-sql-advance-query-fill-the-gap-of-missing-codes%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Fill%20the%20gap%20of%20Missing%20Codes" 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%2F11%2Fsql-puzzle-sql-advance-query-fill-the-gap-of-missing-codes%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Fill%20the%20gap%20of%20Missing%20Codes" 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%2F11%2Fsql-puzzle-sql-advance-query-fill-the-gap-of-missing-codes%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Fill%20the%20gap%20of%20Missing%20Codes" 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%2F11%2Fsql-puzzle-sql-advance-query-fill-the-gap-of-missing-codes%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Fill%20the%20gap%20of%20Missing%20Codes" data-a2a-url="https://www.dbrnd.com/2017/11/sql-puzzle-sql-advance-query-fill-the-gap-of-missing-codes/" data-a2a-title="SQL Puzzle: SQL Advance Query – Fill the gap of Missing Codes"></a></p><p>Check the below input data and expected output to fill the gap of missing codes.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">Code
----
A
NULL
NULL
NULL
NULL
NULL
M
NULL
NULL
NULL
N
NULL
NULL</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">OldCode NewCode
------- -------
A       A
NULL    A
NULL    A
NULL    A
NULL    A
NULL    A
M       M
NULL    M
NULL    M
NULL    M
N       N
NULL    N
NULL    N</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE MissingData
(
	Code CHAR(1)
)
GO
 
INSERT INTO MissingData
CodeS ('A'),(NULL),(NULL),(NULL),(NULL),(NULL)
,('M'),(NULL),(NULL),(NULL),('N'),(NULL),(NULL)
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE AS
(
       SELECT p.Code
       ,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) rrnk FROM MissingData p 
) 
SELECT
       c.Code AS OldCode
       ,(
          SELECT d.Code from CTE d WHERE d.rrnk = (Select max(e.rrnk) from CTE e 
          Where e.rrnk &lt;= c.rrnk and (e.Code != '' ))
       ) NewCode
FROM CTE c</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/11/sql-puzzle-sql-advance-query-fill-the-gap-of-missing-codes/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Query to Find Missing Number and ID in SQL Server</title>
		<link>https://www.dbrnd.com/2015/06/query-to-find-missing-number-id-sql-server/</link>
					<comments>https://www.dbrnd.com/2015/06/query-to-find-missing-number-id-sql-server/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 20 Jun 2015 10:36:29 +0000</pubDate>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[Missing]]></category>
		<category><![CDATA[Number]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=242</guid>

					<description><![CDATA[In this post, I will give you one full demo to find a missing number of identity column in SQL Server. Before a few days ago, I was working on production report and found that some number is missing. This sequence and missing number are very important. Immediately one requirement come to my desk is, [&#8230;]]]></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%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" 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%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" 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%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" 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%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" 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%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" 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%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&amp;linkname=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" 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%2F2015%2F06%2Fquery-to-find-missing-number-id-sql-server%2F&#038;title=Query%20to%20Find%20Missing%20Number%20and%20ID%20in%20SQL%20Server" data-a2a-url="https://www.dbrnd.com/2015/06/query-to-find-missing-number-id-sql-server/" data-a2a-title="Query to Find Missing Number and ID in SQL Server"></a></p><p>In this post, I will give you one full demo to find a missing number of identity column in SQL Server.</p>
<blockquote><p>Before a few days ago, I was working on production report and found that some number is missing. This sequence and missing number are very important.<br />
Immediately one requirement come to my desk is, please find a list of all missing number for investigation purpose.</p></blockquote>
<p>Below is a full demo script to find the missing number in SQL Server.</p>
<p>First Create one sample table with data.</p><pre class="crayon-plain-tag">/*Create one table to store Numbers*/

CREATE TABLE dbo.tbl_Numbers
(
	ID INTEGER
)
GO

/*Insert some test records. You can also see I have missed many number which we are going to find by query*/

INSERT INTO dbo.tbl_Numbers 
VALUES(1),(2),(3),(4),(6),(7),(9),(10)
,(11),(15),(16),(19),(22),(23),(24)
,(28),(32),(33),(34),(35),(38),(40)
,(41),(42),(43),(44),(45),(47),(49)
,(50)
GO</pre><p>
Below is step by step script to find this missing number.</p><pre class="crayon-plain-tag">/*Step 1: Create one temp table and store all distinct Numbers*/
SELECT DISTINCT ID INTO #DistinctID FROM dbo.tbl_Numbers
GO

/*Step 2: Find NextID which has something missing before it.
Store into one TempTable*/

SELECT 
	(ROW_NUMBER()OVER (ORDER BY ID ASC)) AS TableCountID
	,b.ID 
	,(
		SELECT Top 1 ID
		FROM #DistinctID a WITH (NOLOCK)
		WHERE a.ID &gt; b.ID
	) AS NextID
INTO #NextID
FROM #DistinctID AS b WITH (NOLOCK)
WHERE 
(
	SELECT Top 1 ID
	FROM #DistinctID a WITH (NOLOCK)
	WHERE a.ID &gt; b.ID
) &lt;&gt; b.ID+1
ORDER BY b.ID

/*Step 3: Create one temp table to store missing number details */

CREATE TABLE #MissingID
(
	TableCountID INTEGER
	,ID INTEGER
	,NextID INTEGER
	,MissingID INTEGER
	,TotalDiff INTEGER
)
GO

/*Step 4: Now start to find missing id using WHILE Loop. */

DECLARE @CountID INTEGER
DECLARE @InnerLoopCountID INTEGER
SET @CountID = 1
WHILE (@CountID &amp;gt; 0)
BEGIN
INSERT INTO #MissingID
SELECT 
	TableCountID
	,ID 
	,NextID
	,CASE WHEN TotalDiff = 1 THEN ID+TotalDiff END AS MissingID
	,TotalDiff		
FROM
(
SELECT	
	TableCountID
	,ID
	,NextID
	,(NextID-ID)-1 AS TotalDiff
FROM #NextID
) AS T WHERE TableCountID = @CountID

IF ((SELECT (NextID-ID)-1 FROM #NextID WHERE TableCountID = @CountID)&amp;gt;1)
BEGIN	
	SET @InnerLoopCountID = (SELECT (NextID-ID)-1 FROM #NextID WHERE TableCountID = @CountID)
	
WHILE (@InnerLoopCountID &amp;gt; 0)
BEGIN
	INSERT INTO #MissingID
	SELECT 
		TableCountID
		,ID 
		,NextID
		,ID+@InnerLoopCountID AS MissingNumber
		,TotalDiff		
	FROM
	(
	SELECT	
		TableCountID
		,ID
		,NextID
		,(NextID-ID)-1 AS TotalDiff
	FROM #NextID
	) AS T WHERE TableCountID = @CountID		

	SET @InnerLoopCountID = @InnerLoopCountID - 1				
END			
END
SET @CountID = @CountID+1

IF @CountID = (SELECT COUNT(1)+1 FROM #NextID)
BEGIN	
	RETURN
END
END
GO

/*Step 5: Now select missing data from #MissingID temp table. 
You can find full details on MissingID.*/

SELECT *FROM #MissingID WHERE MissingID IS NOT NULL ORDER BY MissingID 
GO</pre><p>
Result of Step 5:<a href="https://www.dbrnd.com/wp-content/uploads/2015/06/MissingID.png"><img fetchpriority="high" decoding="async" class="aligncenter wp-image-250 size-full" title="Missing number in sql server" src="https://www.dbrnd.com/wp-content/uploads/2015/06/MissingID.png" alt="Missing number in sql server" width="327" height="440" srcset="https://www.dbrnd.com/wp-content/uploads/2015/06/MissingID.png 327w, https://www.dbrnd.com/wp-content/uploads/2015/06/MissingID-223x300.png 223w, https://www.dbrnd.com/wp-content/uploads/2015/06/MissingID-150x202.png 150w" sizes="(max-width: 327px) 100vw, 327px" /></a>In the above result you can find total 20 missing number. MissingID Column represents missing numbers.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/06/query-to-find-missing-number-id-sql-server/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
