<?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>SQL Query &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/sql-query/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>Mon, 24 Jun 2019 19:42:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.5</generator>
	<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Find the Order basis on thier Status and Step</title>
		<link>https://www.dbrnd.com/2019/08/sql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step/</link>
					<comments>https://www.dbrnd.com/2019/08/sql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 12 Aug 2019 16:16:20 +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[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=5809</guid>

					<description><![CDATA[Check the below input data and require output data to find order which step is 0 with status D and for the same order other status are P. Input Data: [crayon-6aafb53e8eb71982007066/] Expected Output: [crayon-6aafb53e8eb76366981028/] Create a table with sample data: [crayon-6aafb53e8eb77381195586/] Solution 1: Using CTE [crayon-6aafb53e8eb78021731409/] Solution 2: Using GROUP BY .. Having [crayon-6aafb53e8eb79076733461/] Please [&#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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" 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%2F2019%2F08%2Fsql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Find%20the%20Order%20basis%20on%20thier%20Status%20and%20Step" data-a2a-url="https://www.dbrnd.com/2019/08/sql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step/" data-a2a-title="SQL Puzzle: SQL Advance Query – Find the Order basis on thier Status and Step"></a></p><p>Check the below input data and require output data to find order which step is 0 with status D and for the same order other status are P.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">OrderID Step        Status
------- ----------- ------
ABC     0           D
ABC     1           P
ABC     2           P
ABC     3           P
XYZ     0           D
XYZ     1           D
EFQ     0           D</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">OrderID
-------
ABC</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Orders
(
	OrderID CHAR(5) 
	,Step INTEGER 
	,Status CHAR(1) 
)
GO 

INSERT INTO tbl_Orders
VALUES
('ABC', 0, 'D'),
('ABC', 1, 'P'),
('ABC', 2, 'P'),
('ABC', 3, 'P'),
('XYZ', 0, 'D'),
('XYZ', 1, 'D'),
('EFQ', 0, 'D'),
('EFQ', 1, 'D')
GO</pre> </p>
<p><strong>Solution 1: Using CTE</strong></p>
<pre class="crayon-plain-tag">with ctetest as 
( 
SELECT OrderID,Step,Status FROM tbl_Orders WHERE Step = 0 and Status = 'D'
)
select distinct a.OrderID from ctetest a inner join tbl_Orders b on a.OrderID = b.OrderID and b.Status = 'P'</pre> </p>
<p><strong>Solution 2: Using GROUP BY .. Having </strong></p>
<pre class="crayon-plain-tag">SELECT OrderID 
FROM tbl_Orders
GROUP BY OrderID
HAVING COUNT(*) =    
COUNT(CASE WHEN Step &lt;&gt; 0 AND Status = 'P' THEN 1 ELSE NULL END) 
+ COUNT(CASE WHEN Step = 0 AND Status = 'D' THEN 1 ELSE NULL END)</pre> </p>
<p><strong><code>Please try the different solutions for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/08/sql-puzzle-sql-advance-query-find-the-order-basis-on-thier-status-and-step/feed/</wfw:commentRss>
			<slash:comments>19</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: How to compare your objects between two Databases</title>
		<link>https://www.dbrnd.com/2019/08/sql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases/</link>
					<comments>https://www.dbrnd.com/2019/08/sql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 05 Aug 2019 16:16:04 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[compare]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[foreign key]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[Primary key]]></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[stored procedures]]></category>
		<category><![CDATA[tables]]></category>
		<category><![CDATA[trigger]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[views]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4094</guid>

					<description><![CDATA[When we are working with different versions of database, database professionals are comparing the objects between databases of SQL Server. They must make sure about that all databases are in sync. Using this T-SQL Script, you can compare two databases and find a list of unmatched objects like: Constraints, Tables, Views, Stored Procedure, Triggers. Sample [&#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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&amp;linkname=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" 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%2F2019%2F08%2Fsql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases%2F&#038;title=SQL%20Server%3A%20How%20to%20compare%20your%20objects%20between%20two%20Databases" data-a2a-url="https://www.dbrnd.com/2019/08/sql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases/" data-a2a-title="SQL Server: How to compare your objects between two Databases"></a></p><p>When we are working with different versions of database, database professionals are comparing the objects between databases of SQL Server.<br />
They must make sure about that all databases are in sync. </p>
<blockquote><p>Using this T-SQL Script, you can compare two databases and find a list of unmatched objects like: Constraints, Tables, Views, Stored Procedure, Triggers. </p></blockquote>
<p><strong>Sample demonstration:</strong><br />
<strong>First, Create two sample databases:<br />
</strong></p>
<pre class="crayon-plain-tag">CREATE DATABASE ABC
GO
CREATE DATABASE XYZ
GO</pre> </p>
<p><strong>Create few sample tables in both the databases:<br />
</strong></p><pre class="crayon-plain-tag">CREATE TABLE ABC.dbo.tbl_Test
(
	ID INT
	,MyDate DATE
)
GO

CREATE TABLE ABC.dbo.tbl_MyTest
(
	ID INT
	,Name VARCHAR(50)
)
GO

CREATE TABLE XYZ.dbo.tbl_Test
(
	ID INT
	,MyDate DATE
)
GO

CREATE TABLE XYZ.dbo.tbl_MyFinal
(
	ID INT
	,Name VARCHAR(50)
)
GO</pre><p> </p>
<p><strong>T-SQL Script to compare objects between two databases:</strong><br />
According to your databases, You can change the name of @SourceDatabase and @DestinationDatabase.</p>
<pre class="crayon-plain-tag">DECLARE @SourceDatabase VARCHAR(50) 
DECLARE @DestinationDatabase VARCHAR(50) 
DECLARE @SQL VARCHAR(MAX) 
     
SELECT @SourceDatabase = 'ABC' 
SELECT @DestinationDatabase = 'XYZ'
     
SELECT @SQL = 
'
	SELECT 
		ISNULL(S.name,D.name) ObjectName
		,CASE  
			WHEN S.object_id IS NULL 
				THEN D.type_desc + '' is missing in the Source Database: '	+ @SourceDatabase + ''' 
			WHEN D.object_id IS NULL 
				THEN S.type_desc +  '' is missing in the Destination Database: ' + @DestinationDatabase + ''' 
		END ''Status'' 
	FROM 
	(
		SELECT * FROM ' + @SourceDatabase + '.SYS.objects  
		WHERE Type_desc not in (''INTERNAL_TABLE'',''SYSTEM_TABLE'',''SERVICE_QUEUE'')
	) AS S  
	FULL OUTER JOIN 
	(
		SELECT * FROM ' + @DestinationDatabase + '.SYS.objects  
		WHERE Type_desc not in (''INTERNAL_TABLE'',''SYSTEM_TABLE'',''SERVICE_QUEUE'')
	) AS D 
	ON S.name = D.name 
		AND S.type = D.type 
	ORDER BY isnull(S.type,D.type)
'  
EXEC (@Sql)</pre> </p>
<p><strong>The Result:</strong></p>
<pre class="crayon-plain-tag">ObjectName                         Status
------------------  -------------------------------------------------------
tbl_Test              NULL
tbl_MyFinal           USER_TABLE is missing in the Source Database: ABC
tbl_MyTest            USER_TABLE is missing in the Destination Database: XYZ</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/08/sql-server-how-to-compare-your-objects-table-view-stored-procedure-trigger-constraint-between-two-databases/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Generate the group of Sequences</title>
		<link>https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-group-of-sequences/</link>
					<comments>https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-group-of-sequences/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 29 Jul 2019 16:16:41 +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[sequence]]></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=5919</guid>

					<description><![CDATA[Check the below input data and expected output to generate the group of sequences. Input Data: [crayon-6aafb53e8f6f4084086205/] Expected Output: [crayon-6aafb53e8f6f7880422320/] Create a table with sample data: [crayon-6aafb53e8f6f9534474645/] Solution: [crayon-6aafb53e8f6fa788718061/] 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-group-of-sequences%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20group%20of%20Sequences" data-a2a-url="https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-group-of-sequences/" data-a2a-title="SQL Puzzle: SQL Advance Query – Generate the group of Sequences"></a></p><p>Check the below input data and expected output to generate the group of sequences. </p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">Name SeqNo
---- -----------
A    1
A    2
A    3
B    1
B    2
C    1
C    2
C    3
D    1
D    2
D    3
D    4
D    5</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">Name SequenceNo_Groups
---- --------------------
A    11,12,13
A    11,12,13
A    11,12,13
B    21,22
B    21,22
C    31,32,33
C    31,32,33
C    31,32,33
D    41,42,43,44,45
D    41,42,43,44,45
D    41,42,43,44,45
D    41,42,43,44,45
D    41,42,43,44,45</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE TestSequences
(
	Name VARCHAR(3)
	,SeqNo INT
)
GO

INSERT INTO TestSequences
VALUES
('A' ,1)
,('A' ,2)
,('A' ,3)
,('B' ,1)
,('B' ,2)
,('C' ,1)
,('C' ,2)
,('C' ,3)
,('D' ,1)
,('D' ,2)
,('D' ,3)
,('D' ,4)
,('D' ,5)
 GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE AS
(
    SELECT b.Name , CAST(rnk  * 10 + SeqNo AS VARCHAR(3)) rnk FROM
    (
        SELECT * , ROW_NUMBER() OVER ( ORDER BY (SELECT NULL) )  rnk  FROM
        (
            SELECT DISTINCT Name FROM TestSequences
        ) a
    ) b
    INNER JOIN TestSequences a on b.Name = a.Name
)
SELECT Name, STUFF ((SELECT ',' + rnk FROM CTE w WHERE w.Name = t.Name FOR XML PATH('')) , 1,1,'') SequenceNo_Groups
FROM CTE t</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/2019/07/sql-puzzle-sql-advance-query-generate-the-group-of-sequences/feed/</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Generate the range of data basis on common combination</title>
		<link>https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination/</link>
					<comments>https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 15 Jul 2019 16:16:14 +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[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>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5940</guid>

					<description><![CDATA[Check the below input data and expected output to generate the report on range basis on the common combination of SendFlag and ReceiveFlag. Input Data: [crayon-6aafb53e8f984568085932/] Expected Output: [crayon-6aafb53e8f987505569469/] Create a table with sample data: [crayon-6aafb53e8f988302204707/] Solution: [crayon-6aafb53e8f989792396479/] 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" 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%2F2019%2F07%2Fsql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Generate%20the%20range%20of%20data%20basis%20on%20common%20combination" data-a2a-url="https://www.dbrnd.com/2019/07/sql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination/" data-a2a-title="SQL Puzzle: SQL Advance Query – Generate the range of data basis on common combination"></a></p><p>Check the below input data and expected output to generate the report on range basis on the common combination of SendFlag and ReceiveFlag.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">ID          CreationDate SendFlag ReceiveFlag
----------- ------------ -------- -----------
1           2016-08-08   0        0
2           2016-10-01   0        0
3           2016-11-26   1        0
4           2016-12-26   1        0
5           2017-01-08   1        0
6           2017-03-10   1        1
7           2017-06-20   1        1
8           2017-08-15   0        1</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">StartID     EndID       SendFlag ReceiveFlag
----------- ----------- -------- -----------
1           2           0        0
3           5           1        0
6           7           1        1
8           8           0        1</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE Flags
(
	ID INT 
	,CreationDate DATE
	,SendFlag BIT
	,ReceiveFlag BIT
)
GO
 
INSERT INTO Flags 
VALUES
(1,'20160808',0,0)
,(2,'20161001',0,0)
,(3,'20161126',1,0)
,(4,'20161226',1,0)
,(5,'20170108',1,0)
,(6,'20170310',1,1)
,(7,'20170620',1,1)
,(8,'20170815',0,1)
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE1 AS
(
    SELECT f.*, ROW_NUMBER() OVER (ORDER BY f.ID) rnk
    FROM Flags f left JOIN Flags f2 on f.id + 1 = f2.id and f.SendFlag = f2.SendFlag and f.ReceiveFlag  = f2.ReceiveFlag
    WHERE f2.ID IS NULL
),
CTE2 AS
(
    SELECT f2.*,ROW_NUMBER() OVER (ORDER BY f2.ID) rnk
    FROM Flags f RIGHT JOIN Flags f2 on f.id = f2.id - 1 and f.SendFlag = f2.SendFlag and f.ReceiveFlag  = f2.ReceiveFlag
    WHERE f.ID IS NULL
)
SELECT c2.ID as StartID,c1.ID as EndID,c1.SendFlag,c2.ReceiveFlag 
FROM CTE1 c1 INNER JOIN CTE2 c2 on c1.rnk = c2.rnk</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/2019/07/sql-puzzle-sql-advance-query-generate-the-range-of-data-basis-on-common-combination/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Script to check Network or Connect permission of User</title>
		<link>https://www.dbrnd.com/2019/06/sql-server-script-to-check-network-or-connect-permission-of-user/</link>
					<comments>https://www.dbrnd.com/2019/06/sql-server-script-to-check-network-or-connect-permission-of-user/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 24 Jun 2019 16:16:20 +0000</pubDate>
				<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[dbrnd]]></category>
		<category><![CDATA[endpoints]]></category>
		<category><![CDATA[server_permissions]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7263</guid>

					<description><![CDATA[In this post, I am sharing a script for checking the Network and Connect related permission of SQL Server User. When you installed the SQL Server, for network access you should enable the TCP/IP pipeline and other connect permission so that others can access the server. Using below script, you can easily identify those permissions [&#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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" 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%2F2019%2F06%2Fsql-server-script-to-check-network-or-connect-permission-of-user%2F&#038;title=SQL%20Server%3A%20Script%20to%20check%20Network%20or%20Connect%20permission%20of%20User" data-a2a-url="https://www.dbrnd.com/2019/06/sql-server-script-to-check-network-or-connect-permission-of-user/" data-a2a-title="SQL Server: Script to check Network or Connect permission of User"></a></p><p>In this post, I am sharing a script for checking the Network and Connect related permission of SQL Server User. </p>
<p>When you installed the SQL Server, for network access you should enable the TCP/IP pipeline and other connect permission so that others can access the server. </p>
<p>Using below script, you can easily identify those permissions which are important for connecting SQL Server. I took the reference from <a href="https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/troubleshoot-always-on-availability-groups-configuration-sql-server" rel="noopener" target="_blank">this</a> official page. </p>
<pre class="crayon-plain-tag">SELECT 
	EP.name, 
	SP.STATE,   
	CONVERT(nvarchar(38), suser_name(SP.grantor_principal_id)) AS GRANTOR,   
	SP.TYPE AS PERMISSION,  
	CONVERT(nvarchar(46),suser_name(SP.grantee_principal_id)) AS GRANTEE   
FROM sys.server_permissions SP, sys.endpoints EP  
WHERE SP.major_id = EP.endpoint_id  
ORDER BY Permission,grantor, grantee</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/06/sql-server-script-to-check-network-or-connect-permission-of-user/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; To generate the account balance column for Bank Accounts</title>
		<link>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/</link>
					<comments>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 17 Jun 2019 16:16:59 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[bank]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></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[transaction]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5860</guid>

					<description><![CDATA[Check the below input data and expected output to prepare the report on bank account transaction like generate the data of AccountBalance column where for CR plus the transaction value and for DR minus the transaction value. Input Data: [crayon-6aafb53e90f8d985901773/] Expected Output: [crayon-6aafb53e90f90653716214/] Create a table with sample data: [crayon-6aafb53e90f91924865359/] Solution: [crayon-6aafb53e90f92725990551/] Please try the [&#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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20To%20generate%20the%20account%20balance%20column%20for%20Bank%20Accounts" data-a2a-url="https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/" data-a2a-title="SQL Puzzle: SQL Advance Query – To generate the account balance column for Bank Accounts"></a></p><p>Check the below input data and expected output to prepare the report on bank account transaction like generate the data of AccountBalance column where for CR plus the transaction value and for DR minus the transaction value.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">TransactionDate AccName    Type Amount
--------------- ---------- ---- -------------
2017-01-01      Anvesh     CR   60000.00
2017-02-01      Anvesh     DB   8000.00
2017-03-01      Anvesh     CR   8000.00
2017-04-01      Anvesh     DB   5000.00
2017-01-01      Nupur      CR   10000.00
2017-02-02      Nupur      CR   8000.00
2017-03-03      Nupur      DB   8000.00</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">TransactionDate AccName    Type Amount                AccountBalance
--------------- ---------- ---- --------------------- ---------------
2017-01-01      Anvesh     CR   60000.00              60000.00
2017-02-01      Anvesh     DB   8000.00               52000.00
2017-03-01      Anvesh     CR   8000.00               60000.00
2017-04-01      Anvesh     DB   5000.00               55000.00
2017-01-01      Nupur      CR   10000.00              10000.00
2017-02-02      Nupur      CR   8000.00               18000.00
2017-03-03      Nupur      DB   8000.00               10000.00</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE AccountBalance
(
	TransactionDate DATE
	,AccName VARCHAR(10)
	,Type VARCHAR(2)
	,Amount MONEY
)
GO

INSERT INTO AccountBalance
VALUES
('2017-01-01','Anvesh','CR','60000')
,('2017-02-01','Anvesh','DB','8000')
,('2017-03-01','Anvesh','CR','8000')
,('2017-04-01','Anvesh','DB','5000')
,('2017-01-01','Nupur','CR','10000')
,('2017-02-02','Nupur','CR','8000')
,('2017-03-03','Nupur','DB','8000')
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE AS
(
    SELECT TransactionDate , AccName , Type 
            , CASE WHEN Type = 'DB' THEN AMOUNT * -1 ELSE AMOUNT END Amount, Amount Amt
            ,ROW_NUMBER() OVER (PARTITION BY AccName ORDER BY (SELECT NULL)) rnk
    FROM AccountBalance
)
SELECT  TransactionDate , AccName , Type , Amt Amount
, (SELECT SUM(Amount) FROM CTE c2 WHERE c2.AccName = c1.AccName AND c2.rnk &lt;= c1.rnk ) AccountBalance
FROM CTE c1</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/2019/06/sql-puzzle-sql-advance-query-to-generate-the-account-balance-column-for-bank-accounts/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Place NULL for repeating values</title>
		<link>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-place-null-for-repeating-values/</link>
					<comments>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-place-null-for-repeating-values/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 03 Jun 2019 16:16:54 +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[Repeat]]></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=5838</guid>

					<description><![CDATA[Check the below input data and expected output to place NULL for repeating values. Input Data: [crayon-6aafb53e916ed372357536/] Expected Output: [crayon-6aafb53e916f1995689397/] Create a table with sample data: [crayon-6aafb53e916f2596799794/] Solution: [crayon-6aafb53e916f3464588127/] Please try the different solutions 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" 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%2F2019%2F06%2Fsql-puzzle-sql-advance-query-place-null-for-repeating-values%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Place%20NULL%20for%20repeating%20values" data-a2a-url="https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-place-null-for-repeating-values/" data-a2a-title="SQL Puzzle: SQL Advance Query – Place NULL for repeating values"></a></p><p>Check the below input data and expected output to place NULL for repeating values.<br />
<strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">A           B           C           Code
----------- ----------- ----------- ----
1           1           1           A
1           1           2           A
1           1           3           A
1           1           4           A
2           2           1           C
2           2           2           C
2           2           3           C</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">A           B           C           Code
----------- ----------- ----------- ----
1           1           1           A
NULL        NULL        2           NULL
NULL        NULL        3           NULL
NULL        NULL        4           NULL
2           2           1           C
NULL        NULL        2           NULL
NULL        NULL        3           NULL</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE Code
(
	A int
	,B Int
	,C int
	,Code CHAR(1) 	
)
GO 
 
INSERT INTO Code
VALUES
(1,1,1,'A')
,(1,1,2,'A')
,(1,1,3,'A')
,(1,1,4,'A')
,(2,2,1,'C')
,(2,2,2,'C')
,(2,2,3,'C')
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;with ctetest as 
(
select *, row_number()over (partition by Code,A,B order by A) as rnk from Code
) 
select
	case when rnk = 1 then A else NULL end as A
	,case when rnk = 1 then B else NULL end AS B
	,C
	,case when rnk = 1 then Code else NULL end AS Code
from ctetest</pre> </p>
<p><strong><code>Please try the different solutions for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/06/sql-puzzle-sql-advance-query-place-null-for-repeating-values/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Script to find the total size of the Indexes</title>
		<link>https://www.dbrnd.com/2019/05/sql-server-script-to-find-the-total-size-of-the-indexes/</link>
					<comments>https://www.dbrnd.com/2019/05/sql-server-script-to-find-the-total-size-of-the-indexes/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 27 May 2019 16:16:18 +0000</pubDate>
				<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[dbrnd]]></category>
		<category><![CDATA[duplicate index]]></category>
		<category><![CDATA[fragmentation]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[redundant index]]></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=4191</guid>

					<description><![CDATA[In this post, I am sharing one script to find the size of Indexes in SQL Server. The first step for performance optimization is to measure the size of tables and indexes which helps you to find out more about fragmentation. I have already shared a couple of articles on SQL Server Indexes like: find [&#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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" 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%2F2019%2F05%2Fsql-server-script-to-find-the-total-size-of-the-indexes%2F&#038;title=SQL%20Server%3A%20Script%20to%20find%20the%20total%20size%20of%20the%20Indexes" data-a2a-url="https://www.dbrnd.com/2019/05/sql-server-script-to-find-the-total-size-of-the-indexes/" data-a2a-title="SQL Server: Script to find the total size of the Indexes"></a></p><p>In this post, I am sharing one script to find the size of Indexes in SQL Server.</p>
<blockquote><p>The first step for performance optimization is to measure the size of tables and indexes which helps you to find out more about fragmentation.</p></blockquote>
<p>I have already shared a couple of articles on SQL Server Indexes like: find duplicate indexes, unused indexes and fragmentation of indexes. </p>
<p>You can also access these articles after below T-SQL. </p>
<pre class="crayon-plain-tag">SELECT
	OBJECT_SCHEMA_NAME(i.OBJECT_ID) AS SchemaName
	,OBJECT_NAME(i.OBJECT_ID) AS TableName
	,i.name AS IndexName
	,8 * SUM(au.used_pages) AS IndexSizeInKB
FROM sys.indexes AS i
JOIN sys.partitions AS p 
	ON p.OBJECT_ID = i.OBJECT_ID 
		AND p.index_id = i.index_id
JOIN sys.allocation_units AS au 
	ON au.container_id = p.partition_id
GROUP BY 
	i.OBJECT_ID
	,i.index_id
	,i.name
ORDER BY OBJECT_NAME(i.OBJECT_ID),i.index_id</pre> </p>
<p><strong>Other related articles:</strong></p>
<blockquote class="wp-embedded-content" data-secret="iWrp53ltXi"><p><a href="https://www.dbrnd.com/2016/04/sql-server-find-the-total-row-count-and-size-of-the-tables/">SQL Server: Find the total row count and size of the Tables</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/04/sql-server-find-the-total-row-count-and-size-of-the-tables/embed/#?secret=iWrp53ltXi" data-secret="iWrp53ltXi" width="600" height="338" title="&#8220;SQL Server: Find the total row count and size of the Tables&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="kFuNrnjgJI"><p><a href="https://www.dbrnd.com/2016/11/sql-server-script-to-find-redundant-and-duplicate-indexes/">SQL Server: Script to find Redundant and Duplicate Indexes</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/11/sql-server-script-to-find-redundant-and-duplicate-indexes/embed/#?secret=kFuNrnjgJI" data-secret="kFuNrnjgJI" width="600" height="338" title="&#8220;SQL Server: Script to find Redundant and Duplicate Indexes&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="CLStlFpaQE"><p><a href="https://www.dbrnd.com/2015/10/sql-server-script-to-find-usage-of-database-indexes/">SQL Server: Script to find usage of Database Indexes</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/10/sql-server-script-to-find-usage-of-database-indexes/embed/#?secret=CLStlFpaQE" data-secret="CLStlFpaQE" width="600" height="338" title="&#8220;SQL Server: Script to find usage of Database Indexes&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="CuRotuXr6g"><p><a href="https://www.dbrnd.com/2016/03/sql-server-script-to-find-index-average-fragmentation-in-percentage/">SQL Server: Script to find Index Average Fragmentation in Percentage</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/03/sql-server-script-to-find-index-average-fragmentation-in-percentage/embed/#?secret=CuRotuXr6g" data-secret="CuRotuXr6g" width="600" height="338" title="&#8220;SQL Server: Script to find Index Average Fragmentation in Percentage&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/05/sql-server-script-to-find-the-total-size-of-the-indexes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: How to find a time when was SQL Server Restarted</title>
		<link>https://www.dbrnd.com/2019/05/sql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces/</link>
					<comments>https://www.dbrnd.com/2019/05/sql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 20 May 2019 16:16:01 +0000</pubDate>
				<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[dbrnd]]></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 Restart]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[sys.databases]]></category>
		<category><![CDATA[sys.dm_exec_sessions]]></category>
		<category><![CDATA[sys.traces]]></category>
		<category><![CDATA[sysdatabases]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4089</guid>

					<description><![CDATA[In this post, I am sharing few different scripts to find when was the SQL Server restarted. The DBAs must know about this because they have to make sure about data consistency. For example: when SQL Server restarted because of power failure, system failure, update failure or any other reason, We must check the last [&#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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&amp;linkname=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" 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%2F2019%2F05%2Fsql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces%2F&#038;title=SQL%20Server%3A%20How%20to%20find%20a%20time%20when%20was%20SQL%20Server%20Restarted" data-a2a-url="https://www.dbrnd.com/2019/05/sql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces/" data-a2a-title="SQL Server: How to find a time when was SQL Server Restarted"></a></p><p>In this post, I am sharing few different scripts to find when was the SQL Server restarted.</p>
<blockquote><p>The DBAs must know about this because they have to make sure about data consistency.
</p></blockquote>
<blockquote><p>For example: when SQL Server restarted because of power failure, system failure, update failure or any other reason, We must check the last restarted time of SQL Server. Because from that time, we have to restore data from our backup disks to the actual disk.</p></blockquote>
<blockquote><p>When SQL Server restarted, few transactions also rollback. We have to also identify these transactions by knowing the correct time when SQL Server actually restarted.
</p></blockquote>
<p><strong>Using sys.dm_exec_sessions:</strong></p>
<pre class="crayon-plain-tag">SELECT login_time FROM sys.dm_exec_sessions WHERE session_id = 1
GO</pre> </p>
<p><strong>Using sys.dm_os_sys_info:</strong></p>
<pre class="crayon-plain-tag">SELECT sqlserver_start_time FROM sys.dm_os_sys_info
GO</pre> </p>
<p><strong>Using sys.databases:</strong></p>
<pre class="crayon-plain-tag">SELECT create_date FROM sys.databases WHERE name = 'tempdb' 
GO</pre> </p>
<p><strong>Using sys.traces:</strong></p>
<pre class="crayon-plain-tag">SELECT start_time FROM sys.traces WHERE is_default = 1  
GO</pre> </p>
<p><strong>Using sysdatabases: </strong></p>
<pre class="crayon-plain-tag">SELECT crdate FROM sysdatabases WHERE name='tempdb'  
GO</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/05/sql-server-how-to-find-a-time-when-was-sql-server-restarted-dm_exec_sessions-dm_os_sys_info-traces/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Report on Manager &#038; Employee Nth level hierarchy</title>
		<link>https://www.dbrnd.com/2019/05/sql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy/</link>
					<comments>https://www.dbrnd.com/2019/05/sql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 06 May 2019 16:16:43 +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[hierarchy]]></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=5865</guid>

					<description><![CDATA[Check the below input data and expected output to prepare the report on Manager -> Employee Nth level hierarchy basis on the ManagerID column. Input Data: [crayon-6aafb53e91a5f026338421/] Expected Output: [crayon-6aafb53e91a60546997299/] Create a table with sample data: [crayon-6aafb53e91a61950621760/] Solution: [crayon-6aafb53e91a62613020032/] 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" 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%2F2019%2F05%2Fsql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Report%20on%20Manager%20%26%20Employee%20Nth%20level%20hierarchy" data-a2a-url="https://www.dbrnd.com/2019/05/sql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy/" data-a2a-title="SQL Puzzle: SQL Advance Query – Report on Manager &amp; Employee Nth level hierarchy"></a></p><p>Check the below input data and expected output to prepare the report on Manager -> Employee Nth level hierarchy basis on the ManagerID column.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">EmpID       EmpName    ManagerID
----------- ---------- -----------
1           Anvesh     NULL
2           Neevan     NULL
3           Mukesh     1
4           Rajesh     3
5           Nupur      2
6           Roy        5
7           Martin     6
8           Manish     1
9           Eric       2
10          Purv       9</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">EmpName    Hierarchy FullHierarchyName
---------- --------- --------------------------
Anvesh     1         Anvesh.
Manish     8         Anvesh..Manish.
Mukesh     3         Anvesh..Mukesh.
Rajesh     4         Anvesh..Mukesh..Rajesh.
Neevan     2         Neevan.
Eric       9         Neevan..Eric.
Purv       10        Neevan..Eric..Purv.
Nupur      5         Neevan..Nupur.
Roy        6         Neevan..Nupur..Roy.
Martin     7         Neevan..Nupur..Roy..Martin.</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE Employees
(
	EmpID INT
	,EmpName VARCHAR(10)
	,ManagerID INT
)
 
INSERT INTO Employees 
VALUES
(1,'Anvesh',NULL)
,(2,'Neevan',NULL)
,(3,'Mukesh',1)
,(4,'Rajesh',3)
,(5,'Nupur',2)
,(6,'Roy',5)
,(7,'Martin',6)
,(8,'Manish',1)
,(9,'Eric',2)
,(10,'Purv',9)</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE(EmpName , EmpId, Level,FullHierarchyName) AS (
     Select E.EmpName, E.EmpID, 0 Level
     ,Cast(E.EmpName+'.' as Varchar(MAX)) FullHierarchyName         
     From Employees E Where E.ManagerID IS NULL
     UNION ALL
     Select E.EmpName, E.EmpID, c.Level + 1 , c.FullHierarchyName+'.'+E.EmpName+'.' FullHierarchyName  
     From Employees E INNER JOIN CTE c on c.EmpID = e.ManagerID 
)
SELECT H.EmpName ,CAST(H.EmpID AS VARCHAR(2)) Hierarchy , FullHierarchyName FROM CTE H 
ORDER BY H.FullHierarchyName</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/2019/05/sql-puzzle-sql-advance-query-report-on-manager-employee-nth-level-hierarchy/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
