Are row_number and partition by available in MySQL? Answer is NO.
Microsoft SQL Server support row_number and partition by to find one unique record from a set of duplicate records. We can also set the order by to get one unique record.
MySQL doesn’t provide this feature directly. We can achieve this by using an inline variable for SELECT statements.
In this post, I provide you full demonstration to perform this exercise.
I have also prepared script to insert sample duplicate records and create one stored procedure to find only unique record base on giving order by.
Below is a full demonstration:
/*Create sample database and table with sample data.*/
CREATE DATABASE `Employee`;
CREATE TABLE Employee.EmpDuplicate
INSERT INTO Employee.EmpDuplicate VALUES
/*Sample Stored Procedure to find Unique record from the group of duplicate.*/
CREATE PROCEDURE Employee.usp_GETUniqueEmployeeRecords()
,@rn1 := IF(@pk1=CONCAT(EmpID,EmpRemark), @rn1+1,1) AS rowNumber
,@pk1 := CONCAT(EmpID,EmpRemark)
) B WHERE rowNumber=1;
/*Execute this Stored Procedure.*/
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.