<?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>Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/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, 17 Sep 2019 19:49:01 +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>PostgreSQL: How to Delete all duplicate rows Except one</title>
		<link>https://www.dbrnd.com/2019/09/postgresql-how-to-delete-all-duplicate-rows-except-one/</link>
					<comments>https://www.dbrnd.com/2019/09/postgresql-how-to-delete-all-duplicate-rows-except-one/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 17 Sep 2019 19:49:01 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[delete duplicate]]></category>
		<category><![CDATA[duplicate record]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[records]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3837</guid>

					<description><![CDATA[I have already written a similar article to delete duplicate records in SQL Server and MySQL. Here, You can also access that articles. Delete all duplicate rows in MySQL Delete all duplicates rows except one in SQL Server Recently, I got one request for one script to delete duplicate records in PostgreSQL. Most of 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%2F09%2Fpostgresql-how-to-delete-all-duplicate-rows-except-one%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Delete%20all%20duplicate%20rows%20Except%20one" 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%2F09%2Fpostgresql-how-to-delete-all-duplicate-rows-except-one%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Delete%20all%20duplicate%20rows%20Except%20one" 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%2F09%2Fpostgresql-how-to-delete-all-duplicate-rows-except-one%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Delete%20all%20duplicate%20rows%20Except%20one" 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%2F09%2Fpostgresql-how-to-delete-all-duplicate-rows-except-one%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Delete%20all%20duplicate%20rows%20Except%20one" 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%2F09%2Fpostgresql-how-to-delete-all-duplicate-rows-except-one%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Delete%20all%20duplicate%20rows%20Except%20one" 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%2F09%2Fpostgresql-how-to-delete-all-duplicate-rows-except-one%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Delete%20all%20duplicate%20rows%20Except%20one" 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%2F09%2Fpostgresql-how-to-delete-all-duplicate-rows-except-one%2F&#038;title=PostgreSQL%3A%20How%20to%20Delete%20all%20duplicate%20rows%20Except%20one" data-a2a-url="https://www.dbrnd.com/2019/09/postgresql-how-to-delete-all-duplicate-rows-except-one/" data-a2a-title="PostgreSQL: How to Delete all duplicate rows Except one"></a></p><p>I have already written a similar article to delete duplicate records in SQL Server and MySQL.<br />
Here, You can also access that articles. </p>
<blockquote class="wp-embedded-content" data-secret="4BibnBnPRG"><p><a href="https://www.dbrnd.com/2015/09/delete-all-duplicate-rows-in-mysql/">Delete all duplicate rows in MySQL</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/09/delete-all-duplicate-rows-in-mysql/embed/#?secret=4BibnBnPRG" data-secret="4BibnBnPRG" width="600" height="338" title="&#8220;Delete all duplicate rows in MySQL&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="hOr3tlfWTl"><p><a href="https://www.dbrnd.com/2015/09/delete-all-duplicates-rows-in-sql-server/">Delete all duplicates rows except one in SQL Server</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/09/delete-all-duplicates-rows-in-sql-server/embed/#?secret=hOr3tlfWTl" data-secret="hOr3tlfWTl" width="600" height="338" title="&#8220;Delete all duplicates rows except one in SQL Server&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote><p>Recently, I got one request for one script to delete duplicate records in PostgreSQL.<br />
Most of the Database Developers have such a requirement to delete duplicate records from the Database.</p></blockquote>
<blockquote><p>Like SQL Server, ROW_NUMBER() PARTITION BY is also available in PostgreSQL.<br />
I have prepared this script, using simple inner query with the use of ROW_NUMBER() PARTITION BY clause. </p></blockquote>
<p><strong>Create a sample table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_RemoveDuplicate
(	
	ID INTEGER PRIMARY KEY
	,Name VARCHAR(150)
);</pre> </p>
<p><strong>Insert few duplicate records:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_RemoveDuplicate VALUES
(1,'ABC'),(2,'XYZ')
,(3,'XYZ'),(4,'RFQ')
,(5,'PQR'),(6,'EFG')
,(7,'EFG'),(8,'ABC');</pre> </p>
<p><strong>Except one, Delete all duplicate records:</strong></p>
<pre class="crayon-plain-tag">DELETE FROM tbl_RemoveDuplicate
WHERE ID IN 
(SELECT ID
FROM (SELECT id,
	     ROW_NUMBER() OVER (partition BY Name ORDER BY ID) AS RowNumber
     FROM tbl_RemoveDuplicate) AS T
WHERE T.RowNumber &gt; 1);</pre> </p>
<p><strong>Check the result:</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM tbl_RemoveDuplicate;
 id | name
----+------
  1 | ABC
  2 | XYZ
  4 | RFQ
  5 | PQR
  6 | EFG
(5 rows)</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/09/postgresql-how-to-delete-all-duplicate-rows-except-one/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<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-6a8eb1d43a6d3644620704/] Expected Output: [crayon-6a8eb1d43a6d5160840551/] Create a table with sample data: [crayon-6a8eb1d43a6d6227667419/] Solution 1: Using CTE [crayon-6a8eb1d43a6d7657172546/] Solution 2: Using GROUP BY .. Having [crayon-6a8eb1d43a6d8969239485/] 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-6a8eb1d43ac56740068037/] Expected Output: [crayon-6a8eb1d43ac57645116160/] Create a table with sample data: [crayon-6a8eb1d43ac58737779087/] Solution: [crayon-6a8eb1d43ac59223970998/] 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>PostgreSQL: Create Index on Full Text Search tsvector Data</title>
		<link>https://www.dbrnd.com/2019/07/postgresql-create-index-on-full-text-search-tsvector-data/</link>
					<comments>https://www.dbrnd.com/2019/07/postgresql-create-index-on-full-text-search-tsvector-data/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 22 Jul 2019 16:16:31 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Full Text Search]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[to_tsquery]]></category>
		<category><![CDATA[tsvector]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6968</guid>

					<description><![CDATA[In this post, I am sharing an example of applying Full Text Search on PostgreSQL Table with Index. Here, I am using tsvector for full text search which is document type and uses match operator like @@. PostgreSQL: Example of Trigram Index for Full Text Search using pg_trgm Extension Below is an example: Create a [&#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%2F07%2Fpostgresql-create-index-on-full-text-search-tsvector-data%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20on%20Full%20Text%20Search%20tsvector%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%2F2019%2F07%2Fpostgresql-create-index-on-full-text-search-tsvector-data%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20on%20Full%20Text%20Search%20tsvector%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%2F2019%2F07%2Fpostgresql-create-index-on-full-text-search-tsvector-data%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20on%20Full%20Text%20Search%20tsvector%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%2F2019%2F07%2Fpostgresql-create-index-on-full-text-search-tsvector-data%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20on%20Full%20Text%20Search%20tsvector%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%2F2019%2F07%2Fpostgresql-create-index-on-full-text-search-tsvector-data%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20on%20Full%20Text%20Search%20tsvector%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%2F2019%2F07%2Fpostgresql-create-index-on-full-text-search-tsvector-data%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20on%20Full%20Text%20Search%20tsvector%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%2F2019%2F07%2Fpostgresql-create-index-on-full-text-search-tsvector-data%2F&#038;title=PostgreSQL%3A%20Create%20Index%20on%20Full%20Text%20Search%20tsvector%20Data" data-a2a-url="https://www.dbrnd.com/2019/07/postgresql-create-index-on-full-text-search-tsvector-data/" data-a2a-title="PostgreSQL: Create Index on Full Text Search tsvector Data"></a></p><p>In this post, I am sharing an example of applying Full Text Search on PostgreSQL Table with Index. </p>
<p>Here, I am using tsvector for full text search which is document type and uses match operator like @@.</p>
<blockquote class="wp-embedded-content" data-secret="FxWul5DgOM"><p><a href="https://www.dbrnd.com/2016/08/postgresql-example-of-trigram-index-for-full-text-search-using-pg_trgm-extension/">PostgreSQL: Example of Trigram Index for Full Text Search using pg_trgm Extension</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/08/postgresql-example-of-trigram-index-for-full-text-search-using-pg_trgm-extension/embed/#?secret=FxWul5DgOM" data-secret="FxWul5DgOM" width="600" height="338" title="&#8220;PostgreSQL: Example of Trigram Index for Full Text Search using pg_trgm Extension&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>Below is an example:</strong></p>
<p><strong>Create a sample table with data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Textdata
(id int, txt character varying);

INSERT INTO tbl_Textdata VALUES
(1,'I am DBA'),(2,'database research &amp; development'),(3,'think for data only')
,(4,'data is important'),(5,'data is a future'),(6,'I am data owner');</pre> </p>
<p><strong>Do Full Text Searching:</strong></p>
<pre class="crayon-plain-tag">SELECT txt
FROM tbl_Textdata
WHERE to_tsvector(txt) @@ to_tsquery('data');</pre> </p>
<p><strong>Result:</strong></p>
<pre class="crayon-plain-tag">txt
--------------------------
'think for data only'
'data is important'
'data is a future'
'I am data owner'</pre> </p>
<p><strong>Apply an Index on require Text Pattern:</strong></p>
<pre class="crayon-plain-tag">CREATE INDEX tbl_Textdata_txt_idx ON tbl_Textdata USING GIN (to_tsvector('english',txt));</pre> </p>
<p><strong>Check the execution plan:</strong></p>
<pre class="crayon-plain-tag">explain analyze
SELECT txt
FROM tbl_Textdata
WHERE to_tsvector(txt) @@ to_tsquery('data');

'Seq Scan on tbl_textdata  (cost=0.00..4.07 rows=1 width=32) (actual time=0.043..0.058 rows=4 loops=1)'
'  Filter: (to_tsvector((txt)::text) @@ to_tsquery('data'::text))'
'  Rows Removed by Filter: 2'
'Planning time: 5.494 ms'
'Execution time: 0.083 ms'</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/07/postgresql-create-index-on-full-text-search-tsvector-data/feed/</wfw:commentRss>
			<slash:comments>0</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-6a8eb1d43af2c264289426/] Expected Output: [crayon-6a8eb1d43af2d941881104/] Create a table with sample data: [crayon-6a8eb1d43af2e209253591/] Solution: [crayon-6a8eb1d43af2f596501471/] 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>Database Theory: What is CAP theorem &#8211; Consistency, Availability, Partition</title>
		<link>https://www.dbrnd.com/2019/07/database-theory-what-is-cap-theorem-consistency-availability-partition/</link>
					<comments>https://www.dbrnd.com/2019/07/database-theory-what-is-cap-theorem-consistency-availability-partition/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 08 Jul 2019 16:16:42 +0000</pubDate>
				<category><![CDATA[Database Theory]]></category>
		<category><![CDATA[ACID Properties]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Availability]]></category>
		<category><![CDATA[CAP theorem]]></category>
		<category><![CDATA[Consistency]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database concept]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[database standards]]></category>
		<category><![CDATA[database topic]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Partition]]></category>
		<category><![CDATA[RDBMS]]></category>
		<category><![CDATA[transaction]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3915</guid>

					<description><![CDATA[We all are aware about the ACID Properties of Database System (Atomicity, Consistency, Isolation, Durability). If you don&#8217;t know about the ACID, please visit below article. ACID Properties in Database System (Atomicity, Consistency, Isolation, Durability) In last one decade, lots of new Database Products available in the Market. The types are: RDBMS Products, NoSQL Products, [&#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%2F07%2Fdatabase-theory-what-is-cap-theorem-consistency-availability-partition%2F&amp;linkname=Database%20Theory%3A%20What%20is%20CAP%20theorem%20%E2%80%93%20Consistency%2C%20Availability%2C%20Partition" 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%2Fdatabase-theory-what-is-cap-theorem-consistency-availability-partition%2F&amp;linkname=Database%20Theory%3A%20What%20is%20CAP%20theorem%20%E2%80%93%20Consistency%2C%20Availability%2C%20Partition" 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%2Fdatabase-theory-what-is-cap-theorem-consistency-availability-partition%2F&amp;linkname=Database%20Theory%3A%20What%20is%20CAP%20theorem%20%E2%80%93%20Consistency%2C%20Availability%2C%20Partition" 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%2Fdatabase-theory-what-is-cap-theorem-consistency-availability-partition%2F&amp;linkname=Database%20Theory%3A%20What%20is%20CAP%20theorem%20%E2%80%93%20Consistency%2C%20Availability%2C%20Partition" 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%2Fdatabase-theory-what-is-cap-theorem-consistency-availability-partition%2F&amp;linkname=Database%20Theory%3A%20What%20is%20CAP%20theorem%20%E2%80%93%20Consistency%2C%20Availability%2C%20Partition" 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%2Fdatabase-theory-what-is-cap-theorem-consistency-availability-partition%2F&amp;linkname=Database%20Theory%3A%20What%20is%20CAP%20theorem%20%E2%80%93%20Consistency%2C%20Availability%2C%20Partition" 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%2Fdatabase-theory-what-is-cap-theorem-consistency-availability-partition%2F&#038;title=Database%20Theory%3A%20What%20is%20CAP%20theorem%20%E2%80%93%20Consistency%2C%20Availability%2C%20Partition" data-a2a-url="https://www.dbrnd.com/2019/07/database-theory-what-is-cap-theorem-consistency-availability-partition/" data-a2a-title="Database Theory: What is CAP theorem – Consistency, Availability, Partition"></a></p><p>We all are aware about the ACID Properties of Database System (Atomicity, Consistency, Isolation, Durability).<br />
If you don&#8217;t know about the ACID, please visit below article.</p>
<blockquote class="wp-embedded-content" data-secret="aJi5vV3iRV"><p><a href="https://www.dbrnd.com/2015/05/acid-properties-in-database-system/">ACID Properties in Database System (Atomicity, Consistency, Isolation, Durability)</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/05/acid-properties-in-database-system/embed/#?secret=aJi5vV3iRV" data-secret="aJi5vV3iRV" width="600" height="338" title="&#8220;ACID Properties in Database System (Atomicity, Consistency, Isolation, Durability)&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote><p>In last one decade, lots of new Database Products available in the Market. The types are: RDBMS Products, NoSQL Products, BIGDATA, MPP Products.</p></blockquote>
<p><strong>My Question is, how do you choose best database product for your project?</strong></p>
<blockquote><p>Traditional Answer, The first preference of Database Designer would be ACID Properties oriented Databases.</p></blockquote>
<blockquote><p>But most of the NoSQL Databases are not providing full support of ACID Properties, then also NoSQL Databases are popular and People are using it for different purposes.</p></blockquote>
<blockquote><p>Because most of the NoSQL Databases are not ACID oriented, but they are CAP oriented.</p></blockquote>
<p><strong>What is CAP theorem (Consistency, Availability, Partition)?</strong></p>
<blockquote><p>CAP Published by Eric Brewer in 2000, the theorem is a set of basic requirements that describe any distributed system like: NoSQL Cassandra, MongoDB, CouchDB.</p>
<p><img loading="lazy" decoding="async" class="alignright wp-image-3920 size-medium" title="CAP_ConsistencyAvailabilityPartition" src="https://www.dbrnd.com/wp-content/uploads/2016/11/CAP-Theory-254x300.jpg" alt="cap-theory" width="254" height="300" srcset="https://www.dbrnd.com/wp-content/uploads/2016/11/CAP-Theory-254x300.jpg 254w, https://www.dbrnd.com/wp-content/uploads/2016/11/CAP-Theory-750x886.jpg 750w, https://www.dbrnd.com/wp-content/uploads/2016/11/CAP-Theory-150x177.jpg 150w, https://www.dbrnd.com/wp-content/uploads/2016/11/CAP-Theory.jpg 768w" sizes="auto, (max-width: 254px) 100vw, 254px" /></p></blockquote>
<p><strong>Consistency:</strong></p>
<blockquote><p>All nodes can see the same data at the same time. The data are the same across the cluster, so you can read or write any node.</p></blockquote>
<blockquote><p>Each transaction start and end in consistent stats so that all nodes can access the same data. Actually, It is achieved by update all nodes before reading.</p></blockquote>
<p><strong>Availability:</strong></p>
<blockquote><p>Here, The meaning of Availability is to access cluster in any situation like: server goes down, server delay. The system should always respond to a request, even if data is not in sync or inconsistent state, It prints the message like &#8220;This cluster data are not updated&#8221;.</p></blockquote>
<p><strong>Partition:</strong></p>
<blockquote><p>Here, the meaning of Partition is to avoid any type of system failure and system should run with available nodes or data. It does not allow any single point of failure because data replicated across all the clusters.</p></blockquote>
<blockquote><p>The Partition can work with Consistency (CP) or can work with Availability (AP) means, you can never find any Database System which follows the full CAP theorem.</p></blockquote>
<p><strong>Storage System with Partition and Consistency (CP):</strong><br />
Redis, MongoDB, AppFabric Caching, and MemcacheDB.<br />
<strong>Storage System with Partition and Availability (AP):</strong><br />
Cassandra, DynamoDB, and CouchDB.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/07/database-theory-what-is-cap-theorem-consistency-availability-partition/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: Example of SERIALIZABLE Isolation Level</title>
		<link>https://www.dbrnd.com/2019/07/postgresql-example-of-serializable-isolation-level-concurrency-control-of-transaction/</link>
					<comments>https://www.dbrnd.com/2019/07/postgresql-example-of-serializable-isolation-level-concurrency-control-of-transaction/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 01 Jul 2019 16:16:18 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Concurrency Control]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Isolation level]]></category>
		<category><![CDATA[lock table]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[SERIALIZABLE Isolation Level]]></category>
		<category><![CDATA[transaction]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3831</guid>

					<description><![CDATA[THE SERIALIZABLE Isolation level is one kind of extended version of the REPEATABLE READ Isolation level. Now with the SERIALIZABLE Isolation level, you cannot modify the data while another transaction is reading the same data. For SELECT-only transactions, use the SERIALIZABLE isolation level when you do not want to see the other transaction commits during [&#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%2F07%2Fpostgresql-example-of-serializable-isolation-level-concurrency-control-of-transaction%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20SERIALIZABLE%20Isolation%20Level" 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%2Fpostgresql-example-of-serializable-isolation-level-concurrency-control-of-transaction%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20SERIALIZABLE%20Isolation%20Level" 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%2Fpostgresql-example-of-serializable-isolation-level-concurrency-control-of-transaction%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20SERIALIZABLE%20Isolation%20Level" 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%2Fpostgresql-example-of-serializable-isolation-level-concurrency-control-of-transaction%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20SERIALIZABLE%20Isolation%20Level" 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%2Fpostgresql-example-of-serializable-isolation-level-concurrency-control-of-transaction%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20SERIALIZABLE%20Isolation%20Level" 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%2Fpostgresql-example-of-serializable-isolation-level-concurrency-control-of-transaction%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20SERIALIZABLE%20Isolation%20Level" 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%2Fpostgresql-example-of-serializable-isolation-level-concurrency-control-of-transaction%2F&#038;title=PostgreSQL%3A%20Example%20of%20SERIALIZABLE%20Isolation%20Level" data-a2a-url="https://www.dbrnd.com/2019/07/postgresql-example-of-serializable-isolation-level-concurrency-control-of-transaction/" data-a2a-title="PostgreSQL: Example of SERIALIZABLE Isolation Level"></a></p><p>THE SERIALIZABLE Isolation level is one kind of extended version of the REPEATABLE READ Isolation level.<br />
Now with the SERIALIZABLE Isolation level, you cannot modify the data while another transaction is reading the same data.</p>
<blockquote><p>For SELECT-only transactions, use the SERIALIZABLE isolation level when you do not want to see the other transaction commits during your transaction. </p>
<p>For UPDATE and DELETE transactions, SERIALIZABLE isolation prevents concurrent modification of the same data row; it should therefore be used with caution.</p></blockquote>
<p><strong>Create a table with few sample records:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Employee
(
	EmpID INTEGER
	,EmpName VARCHAR(50)
);
 
INSERT INTO tbl_Employee
VALUES 
(1,'Anvesh'),(2,'Neevan')
,(3,'Roy'),(4,'Martin');</pre> </p>
<p><strong>Execute in this current session. e.g. Session &#8211; A:</strong></p>
<pre class="crayon-plain-tag">BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;  

UPDATE tbl_Employee SET EmpName ='Varlin' WHERE EmpID=4;

SELECT *FROM tbl_Employee;

 empid | empname
-------+---------
     1 | Anvesh
     2 | Neevan
     3 | Roy
     4 | Varlin</pre> </p>
<p><strong>Execute in anohter session, e.g. Session &#8211; B:<br />
</strong></p>
<pre class="crayon-plain-tag">UPDATE tbl_Employee SET EmpName ='Loother' WHERE EmpID=3;

SELECT *FROM tbl_Employee;

 empid | empname
-------+---------
     1 | Anvesh
     2 | Neevan
     4 | Varlin
     3 | Loother</pre> </p>
<p><strong>Execute in anohter session, e.g. Session &#8211; C:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_Employee
VALUES (5,'Mahi');

SELECT *FROM tbl_Employee;

 empid | empname
-------+---------
     1 | Anvesh
     2 | Neevan
     4 | Martin
     3 | Loother
     5 | Mahi</pre> </p>
<p><strong>Execute COMMIT on first Current Session &#8211; A:</strong></p>
<pre class="crayon-plain-tag">--Nothing happen to this Query and Data is as it is. 

SELECT *FROM tbl_Employee; 

 empid | empname
-------+---------
     1 | Anvesh
     2 | Neevan
     3 | Roy
     4 | Varlin

-- Now Execute COMMIT:
COMMIT TRANSACTION;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/07/postgresql-example-of-serializable-isolation-level-concurrency-control-of-transaction/feed/</wfw:commentRss>
			<slash:comments>1</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-6a8eb1d43bd1d799957290/] Expected Output: [crayon-6a8eb1d43bd1e847344485/] Create a table with sample data: [crayon-6a8eb1d43bd1f780954495/] Solution: [crayon-6a8eb1d43bd20378193705/] 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>
	</channel>
</rss>
