USe of Row Number

CREATE TABLE TableOfShame
(
ShameCode varchar(4) NULL,
ShameType varchar(15) NULL,
ShamePriority varchar(10) NULL
);

SELECT *FROM TABLEOFSHAME

INSERT INTO TableOfShame(ShameCode,ShameType,ShamePriority)
SELECT ’01’, ‘Chagrin’, ‘Low’ UNION aLL
SELECT ’01’, ‘Chagrin’, ‘Low’ UNION aLL
SELECT ’02’, ‘Disgust’, ‘High’ UNION aLL
SELECT ’03’, ‘Abashment’,’Medium’ UNION aLL
SELECT ’04’, ‘Embarassment’,’Low’ UNION ALL
SELECT ’05’, ‘Humiliation’, ‘Medium’

these record insert again

WITH cte_FindDuplicateShame as
(
SELECT ShameCode, ShameType, ShamePriority,
ROW_NUMBER() over(PARTITION BY ShameCode, ShameType ORDER BY ShameCode DESC) as RowNum
FROM dbo.TableOfShame
)
SELECT ShameCode, ShameType, ShamePriority, RowNum
FROM cte_FindDuplicateShame
ORDER BY ShameCode, ShameType, RowNum;

WITH cte_FindDuplicateShame as
(
SELECT ShameCode, ShameType, ShamePriority,
ROW_NUMBER() over(PARTITION BY ShameCode, ShameType ORDER BY ShameCode DESC) RowNum
FROM dbo.TableOfShame
)
DELETE cte_FindDuplicateShame
WHERE RowNum 1;

SELECT * FROM TableOfShame ORDER BY ShameCode;

Be happy and enjoy in the programing world

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s