<?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>ROW_NUMBER &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/row_number/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, 05 Feb 2018 20:08:05 +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 Server: Maths of ROW_NUMBER, RANK, DENSE_RANK</title>
		<link>https://www.dbrnd.com/2018/02/sql-server-maths-of-row_number-rank-dense_rank/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 19 Feb 2018 16:16:35 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[dense_rank()]]></category>
		<category><![CDATA[RANK]]></category>
		<category><![CDATA[ROW_NUMBER]]></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=7120</guid>

					<description><![CDATA[In this post, I am comparing and sharing maths on ROW_NUMBER(), RANK(), and DENSE_RANK() of SQL Server. We are using these functions in day to day work, but still few people are not aware of it. Something like, Can we use DENSE_RANK() as ROW_NUMBER()? The answer is yes, we can use for DISTINCT records because [&#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%2F2018%2F02%2Fsql-server-maths-of-row_number-rank-dense_rank%2F&amp;linkname=SQL%20Server%3A%20Maths%20of%20ROW_NUMBER%2C%20RANK%2C%20DENSE_RANK" 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%2F2018%2F02%2Fsql-server-maths-of-row_number-rank-dense_rank%2F&amp;linkname=SQL%20Server%3A%20Maths%20of%20ROW_NUMBER%2C%20RANK%2C%20DENSE_RANK" 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%2F2018%2F02%2Fsql-server-maths-of-row_number-rank-dense_rank%2F&amp;linkname=SQL%20Server%3A%20Maths%20of%20ROW_NUMBER%2C%20RANK%2C%20DENSE_RANK" 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%2F2018%2F02%2Fsql-server-maths-of-row_number-rank-dense_rank%2F&amp;linkname=SQL%20Server%3A%20Maths%20of%20ROW_NUMBER%2C%20RANK%2C%20DENSE_RANK" 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%2F2018%2F02%2Fsql-server-maths-of-row_number-rank-dense_rank%2F&amp;linkname=SQL%20Server%3A%20Maths%20of%20ROW_NUMBER%2C%20RANK%2C%20DENSE_RANK" 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%2F2018%2F02%2Fsql-server-maths-of-row_number-rank-dense_rank%2F&amp;linkname=SQL%20Server%3A%20Maths%20of%20ROW_NUMBER%2C%20RANK%2C%20DENSE_RANK" 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%2F2018%2F02%2Fsql-server-maths-of-row_number-rank-dense_rank%2F&#038;title=SQL%20Server%3A%20Maths%20of%20ROW_NUMBER%2C%20RANK%2C%20DENSE_RANK" data-a2a-url="https://www.dbrnd.com/2018/02/sql-server-maths-of-row_number-rank-dense_rank/" data-a2a-title="SQL Server: Maths of ROW_NUMBER, RANK, DENSE_RANK"></a></p><p>In this post, I am comparing and sharing maths on ROW_NUMBER(), RANK(), and DENSE_RANK() of SQL Server. We are using these functions in day to day work, but still few people are not aware of it. </p>
<blockquote><p>Something like, Can we use DENSE_RANK() as ROW_NUMBER()?<br />
The answer is yes, we can use for DISTINCT records because using ROW_NUMBER(), you cannot generate distinct records as it applicable for all records. </p></blockquote>
<p><strong>Check the below samples, and try it yourself: </strong></p>
<p><strong>Create a table with sample records:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_numbers (name varchar(10))
GO

INSERT INTO tbl_numbers 
VALUES ('ABC'),('ABC'),('ABC')
,('XYZ'),('XYZ'),('LMN'),('OPQ'),('OPQ')
GO</pre> </p>
<p><strong>Compare the output of Row_Number, Rank, DENSE_RANK:</strong></p>
<pre class="crayon-plain-tag">SELECT 
	name
	,ROW_NUMBER() OVER(ORDER BY name) AS RowNumber
	,RANK() OVER(ORDER BY name) AS RankNumber
	,DENSE_RANK() OVER(ORDER BY name) AS DENSE_RankNumber
FROM tbl_numbers</pre> </p>
<p><strong>Result:</strong></p>
<pre class="crayon-plain-tag">name       RowNumber            RankNumber           DENSE_RankNumber
---------- -------------------- -------------------- --------------------
ABC        1                    1                    1
ABC        2                    1                    1
ABC        3                    1                    1
LMN        4                    4                    2
OPQ        5                    5                    3
OPQ        6                    5                    3
XYZ        7                    7                    4
XYZ        8                    7                    4</pre> </p>
<p><strong>Using ROW_NUMBER, you cannot get the distinct records:</strong></p>
<pre class="crayon-plain-tag">SELECT DISTINCT
	name
	,ROW_NUMBER() OVER(ORDER BY name) AS RowNumber
FROM tbl_numbers</pre> </p>
<p><strong>Result:</strong></p>
<pre class="crayon-plain-tag">name       RowNumber
---------- ------------
ABC        1
ABC        2
ABC        3
LMN        4
OPQ        5
OPQ        6
XYZ        7
XYZ        8</pre> </p>
<p><strong>Using CTE, get the distinct records:</strong></p>
<pre class="crayon-plain-tag">WITH CTE AS
(
SELECT DISTINCT
	name
FROM tbl_numbers
)
SELECT 
	name
	,ROW_NUMBER() OVER(ORDER BY name) AS RowNumber
FROM CTE</pre> </p>
<p><strong>Result:</strong></p>
<pre class="crayon-plain-tag">name       RowNumber
---------- -----------
ABC        1
LMN        2
OPQ        3
XYZ        4</pre> </p>
<p><strong>Using DENSE_RANK, get the distinct records:</strong><br />
This query is faster than, CTE and you can use DENSE_RANK as a ROW_NUMBER for distinct Records.</p>
<pre class="crayon-plain-tag">SELECT DISTINCT
	name
	,DENSE_RANK() OVER(ORDER BY name) AS DENSE_RankNumber
FROM tbl_numbers</pre> </p>
<p><strong>Result:</strong></p>
<pre class="crayon-plain-tag">name       DENSE_RankNumber
---------- --------------------
ABC        1
LMN        2
OPQ        3
XYZ        4</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>row_number and partition by in Mysql</title>
		<link>https://www.dbrnd.com/2015/08/row_number-and-partition-by-in-mysql/</link>
					<comments>https://www.dbrnd.com/2015/08/row_number-and-partition-by-in-mysql/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 15 Aug 2015 06:56:53 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<category><![CDATA[partiion by]]></category>
		<category><![CDATA[ROW_NUMBER]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=384</guid>

					<description><![CDATA[Are row_number and partition by available in MySQL? Answer is NO. Microsoft SQL Server support row_number and partition by for finding the unique record from a set of duplicate records. We can also set the order of data. MySQL doesn&#8217;t provide this feature directly. We can achieve this by using an inline variable for SELECT [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Frow_number-and-partition-by-in-mysql%2F&amp;linkname=row_number%20and%20partition%20by%20in%20Mysql" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Frow_number-and-partition-by-in-mysql%2F&amp;linkname=row_number%20and%20partition%20by%20in%20Mysql" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Frow_number-and-partition-by-in-mysql%2F&amp;linkname=row_number%20and%20partition%20by%20in%20Mysql" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Frow_number-and-partition-by-in-mysql%2F&amp;linkname=row_number%20and%20partition%20by%20in%20Mysql" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Frow_number-and-partition-by-in-mysql%2F&amp;linkname=row_number%20and%20partition%20by%20in%20Mysql" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Frow_number-and-partition-by-in-mysql%2F&amp;linkname=row_number%20and%20partition%20by%20in%20Mysql" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Frow_number-and-partition-by-in-mysql%2F&#038;title=row_number%20and%20partition%20by%20in%20Mysql" data-a2a-url="https://www.dbrnd.com/2015/08/row_number-and-partition-by-in-mysql/" data-a2a-title="row_number and partition by in Mysql"></a></p><p>Are row_number and partition by available in MySQL? </p>
<p><strong>Answer is NO.</strong></p>
<p>Microsoft SQL Server support row_number and partition by for finding the unique record from a set of duplicate records. We can also set the order of data. </p>
<blockquote><p>MySQL doesn&#8217;t provide this feature directly. We can achieve this by using an inline variable for SELECT statements.</p></blockquote>
<p><strong>Below is a full demonstration: </strong></p>
<p>I prepared a few scripts for sample data and stored procedure for finding the only unique records.</p>
<p><strong>Below is a full demonstration:</strong></p>
<pre class="crayon-plain-tag">/*Create sample database and table with sample data.*/
CREATE DATABASE `Employee`;

CREATE TABLE Employee.EmpDuplicate
(
	EmpRemarkID INTEGER
	,EmpID INTEGER
    ,EmpRemark VARCHAR(50)
);

INSERT INTO Employee.EmpDuplicate VALUES
(1,25,'abc'),(2,25,'abc'),(3,25,'abc')
,(4,26,'xyz'),(5,27,'pqr'),(6,27,'pqr')
,(7,27,'pqr'),(8,27,'pqr'),(9,28,'xvk')
,(10,28,'xvk'),(11,29,'pqr'),(12,29,'pqr')
,(13,29,'abc'),(14,30,'abc');</pre>
<pre class="crayon-plain-tag">/*Sample Stored Procedure to find Unique record from the group of duplicate.*/
DELIMITER $$
CREATE PROCEDURE Employee.usp_GETUniqueEmployeeRecords()
BEGIN

    SET @rn1:=1;
    SET @pk1:='';    				
    SELECT  
        EmpRemarkID
	,EmpID
	,EmpRemark				
    FROM
    (
	SELECT  
	   EmpRemarkID
	   ,EmpID
	   ,EmpRemark
	   ,@rn1 := IF(@pk1=CONCAT(EmpID,EmpRemark), @rn1+1,1) AS rowNumber
	   ,@pk1 := CONCAT(EmpID,EmpRemark)         
	FROM
	(
	    SELECT  
         	EmpRemarkID
		,EmpID
		,EmpRemark
	    FROM Employee.EmpDuplicate
	) A
    ) B WHERE rowNumber=1;			
END$$
DELIMITER ;</pre>
<pre class="crayon-plain-tag">/*Execute this Stored Procedure.*/
CALL Employee.usp_GETUniqueEmployeeRecords()</pre>
<p><strong>Results:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLUniuqeRecords.png"><img fetchpriority="high" decoding="async" class="alignnone wp-image-391 size-full" title="MySQL Uniuqe Records" src="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLUniuqeRecords.png" alt="MySQLUniuqeRecords" width="297" height="229" srcset="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLUniuqeRecords.png 297w, https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLUniuqeRecords-150x116.png 150w" sizes="(max-width: 297px) 100vw, 297px" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/08/row_number-and-partition-by-in-mysql/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Copy unique records from one table to another in SQL Server</title>
		<link>https://www.dbrnd.com/2015/06/copy-unique-records-from-one-table-to-another-sql-server/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 01 Jun 2015 17:39:07 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[PARTITION BY]]></category>
		<category><![CDATA[ROW_NUMBER]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[SQL SQL Query]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[unique records]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=231</guid>

					<description><![CDATA[In this post, I am sharing a script to copy a unique record from one table to another. You can perform this activity using ROW_NUMBER() and PARTITION BY clause. This partition clause identified set of duplicate records and row_number assign an ID for this set. Below is a full demonstration: Let&#8217;s first create table with [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fcopy-unique-records-from-one-table-to-another-sql-server%2F&amp;linkname=Copy%20unique%20records%20from%20one%20table%20to%20another%20in%20SQL%20Server" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fcopy-unique-records-from-one-table-to-another-sql-server%2F&amp;linkname=Copy%20unique%20records%20from%20one%20table%20to%20another%20in%20SQL%20Server" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fcopy-unique-records-from-one-table-to-another-sql-server%2F&amp;linkname=Copy%20unique%20records%20from%20one%20table%20to%20another%20in%20SQL%20Server" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fcopy-unique-records-from-one-table-to-another-sql-server%2F&amp;linkname=Copy%20unique%20records%20from%20one%20table%20to%20another%20in%20SQL%20Server" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fcopy-unique-records-from-one-table-to-another-sql-server%2F&amp;linkname=Copy%20unique%20records%20from%20one%20table%20to%20another%20in%20SQL%20Server" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fcopy-unique-records-from-one-table-to-another-sql-server%2F&amp;linkname=Copy%20unique%20records%20from%20one%20table%20to%20another%20in%20SQL%20Server" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F06%2Fcopy-unique-records-from-one-table-to-another-sql-server%2F&#038;title=Copy%20unique%20records%20from%20one%20table%20to%20another%20in%20SQL%20Server" data-a2a-url="https://www.dbrnd.com/2015/06/copy-unique-records-from-one-table-to-another-sql-server/" data-a2a-title="Copy unique records from one table to another in SQL Server"></a></p><p>In this post, I am sharing a script to copy a unique record from one table to another.</p>
<p>You can perform this activity using ROW_NUMBER() and PARTITION BY clause.</p>
<p>This partition clause identified set of duplicate records and row_number assign an ID for this set.</p>
<p>Below is a full demonstration:</p>
<p><strong>Let&#8217;s first create table with some records:</strong></p><pre class="crayon-plain-tag">CREATE TABLE dbo.tbl_First
(
 ID INTEGER IDENTITY(1,1)
 ,Name VARCHAR(250)
)
GO
INSERT INTO dbo.tbl_First(Name) VALUES('ABC'),('XYZ'),('ABC'),('EFG'),('PQR'),('EFG'),('RND'),('YFQ'),('XVK'),('ABC')
GO
SELECT *FROM dbo.tbl_First ORDER BY Name 
GO</pre><p>
<p><strong>Result of above select statement is:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/06/SQLResult1.png"><img decoding="async" class="aligncenter wp-image-232 size-full" title="copy unique record sql server" src="https://www.dbrnd.com/wp-content/uploads/2015/06/SQLResult1.png" alt="copy unique record sql server" width="121" height="241" /></a><b>You can see ABC and EFG are duplicate name.</b></p>
<p><strong>Create a second table with copy unique data from the first table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE dbo.tbl_Second
(
 ID INTEGER 
 ,Name VARCHAR(250)
)
GO
 
INSERT INTO dbo.tbl_Second
SELECT 
 ID
 ,Name   
FROM
(
 SELECT 
  ID
  ,Name
  ,ROW_NUMBER() OVER (PARTITION BY Name ORDER BY ID) AS RowNumber
 FROM  dbo.tbl_First AS T
)AS T WHERE T.RowNumber = 1 
GO

SELECT *FROM dbo.tbl_Second
GO</pre>
<p><strong>The result of above second table is:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/06/SQLResult2.png"><img decoding="async" class="aligncenter wp-image-233 size-full" title="copy unique record sql server" src="https://www.dbrnd.com/wp-content/uploads/2015/06/SQLResult2.png" alt="copy unique record sql server" width="108" height="186" /></a>Now you can see all names are unique and copied only seven unique records.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
