Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using Ranking Functions to Deduplicate Data


Using Ranking Functions to Deduplicate Data

Author
Message
Langston Montgomery
Langston Montgomery
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 508
Very nice article. Thanks for sharing. My two cents are that I would use the dense_rank() function to return the rank in tandem order. The rank() function tends to skip a rank depending on how many members belong to a group.

declare @AlphaList table (AlphaKey char);

insert into @AlphaList(AlphaKey) values ('A');
insert into @AlphaList(AlphaKey) values ('A');
insert into @AlphaList(AlphaKey) values ('B');
insert into @AlphaList(AlphaKey) values ('B');
insert into @AlphaList(AlphaKey) values ('C');
insert into @AlphaList(AlphaKey) values ('D');
insert into @AlphaList(AlphaKey) values ('D');
insert into @AlphaList(AlphaKey) values ('E');

select
RANK() over (order by AlphaKey) as Rank
, dense_RANK() over (order by AlphaKey) as [Rank by density]
, ROW_NUMBER() over (order by AlphaKey) as RowNumber
, AlphaKey
from @AlphaList;




Rank Rank by density RowNumber AlphaKey
1 1 1 A
1 1 2 A
3 2 3 B
3 2 4 B
5 3 5 C
6 4 6 D
6 4 7 D
8 5 8 E

Andre Guerreiro
Andre Guerreiro
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1065 Visits: 1514
I'm still getting ranking functions into my head slowly. :-D

Nice article about a very useful way to use them.

Best regards,

Andre Guerreiro Neto

Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
lgidon
lgidon
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 59
I'm disappointed. I've been doing this for a while with ROW_NUMBER and PARTION and it works gr8 in all the situations I've come across.

I can't figure out what advantage I can gain using the RANK function.
I'm not sure there is an advantage, but I'll be happy if someone corrects me :-)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45062 Visits: 39903
I'm torn... it's a nicely written article but the premise is at least twice as complicated as it needs to be and 2 times as slow as conventional methods, IMHO. Don't take my word for it, though... test it yourself...


--===========================================================================
-- Create a multi-million row test table. This is not a measured
-- part of the test.
--===========================================================================
--===== Conditionally drop the test table to make reruns easier
IF OBJECT_ID('TempDB..#AlphaList','U') IS NOT NULL
DROP TABLE #AlphaList
;
--===== Create and populate a multi-million row test table on the fly
SELECT TOP 5000000
AlphaListID = IDENTITY(INT,1,1),
AlphaKey = CHAR(ABS(CHECKSUM(NEWID()))%26+65) --A thru Z randomly
INTO #AlphaList
FROM Master.sys.All_Columns t1
CROSS JOIN Master.sys.All_Columns t2
;
--===== Create the expected indexes
ALTER TABLE #AlphaList
ADD PRIMARY KEY CLUSTERED (AlphaListID)
;
CREATE NONCLUSTERED INDEX IX_#AlphaList_AlphaKey
ON #AlphaList (AlphaKey)
;
--===========================================================================
-- Test 3 different methods with CPU and Duration measurements.
--===========================================================================
--===== "Clear the guns"
PRINT REPLICATE('=',80);
PRINT '========== Traditional RowNumber Method ==========';
DBCC FREEPROCCACHE;
DBCC DROPCLEANBUFFERS;
SET STATISTICS TIME ON;

--===== Test the code
select AlphaKey
from (
select row_number() over(partition by AlphaKey order by AlphaKey) rownum,
AlphaKey
from #AlphaList
) al
where rownum = 1
order by AlphaKey
;
SET STATISTICS TIME OFF;

--===== "Clear the guns"
PRINT REPLICATE('=',80);
PRINT '========== Simple Distinct ==========';
DBCC FREEPROCCACHE;
DBCC DROPCLEANBUFFERS;
SET STATISTICS TIME ON;

--===== Test the code
SELECT DISTINCT AlphaKey
FROM #AlphaList
ORDER BY AlphaKey
;
SET STATISTICS TIME OFF;

--===== "Clear the guns"
PRINT REPLICATE('=',80);
PRINT '========== New Rank Method from Article ==========';
DBCC FREEPROCCACHE;
DBCC DROPCLEANBUFFERS;
SET STATISTICS TIME ON;

--===== Test the code
with AlphaRank(Rank, RowNumber, AlphaKey) as (
select
RANK() over (order by AlphaKey) as Rank
, ROW_NUMBER() over (order by AlphaKey) as RowNumber
, AlphaKey
from #AlphaList
)
select AlphaKey
from AlphaRank
where Rank=RowNumber
;
SET STATISTICS TIME OFF;




Here are the results on my 8 year old desktop single p4 CPU running at 1.8Ghz with 1GB of ram on SQL Server 2005 Developer's Edition sp3.


(5000000 row(s) affected)
================================================================================
========== Traditional RowNumber Method ==========
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

(26 row(s) affected)

SQL Server Execution Times:
CPU time = 2875 ms, elapsed time = 3005 ms.
================================================================================
========== Simple Distinct ==========
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

(26 row(s) affected)

SQL Server Execution Times:
CPU time = 2985 ms, elapsed time = 3043 ms.
================================================================================
========== New Rank Method from Article ==========
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

(26 row(s) affected)

SQL Server Execution Times:
CPU time = 7953 ms, elapsed time = 8037 ms.



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
PravB4u
PravB4u
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 600
Hi,
I think we can achieve the same without rank function.

SELECT * FROM GetMissingItems WHERE RowNumber = 1



Praveen D'sa
MCITP - Database Administrator 2008
http://sqlerrors.wordpress.com
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45062 Visits: 39903
PravB4u (7/29/2010)
Hi,
I think we can achieve the same without rank function.

SELECT * FROM GetMissingItems WHERE RowNumber = 1


Ummm.... Not without the "Partition By" part of the OVER clause for ROW_NUMBER in what appears to be a missing CTE from your code.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search