Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Using Ranking Functions to Deduplicate Data Expand / Collapse
Author
Message
Posted Tuesday, July 27, 2010 6:11 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, June 26, 2013 2:53 PM
Points: 32, Visits: 48
I also generally use a partitioned row_number(), ordering on some selection criteria (highest number of x with provider y, etc).

Is the output of Rank defined by ANSI to always support this method in all implementations? My guess is that it's only guaranteed to be in order, but that the density of the default invocation could vary.

Still, this is clever and hadn't occurred to me before, so thanks for expanding my box.
Post #959319
Posted Tuesday, July 27, 2010 6:42 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 1:02 PM
Points: 120, Visits: 296
Nice article. Gives me another way to de-dupe data.
I use almost the same technique, but I have a time stamp associated with each record and this time stamp differs by milliseconds. So I just pick the one which has the latest time stamp. I should point out that when you use Ranking functions on a table that has really large number of records (in my case it was close to 90 millions some times), the query performance can degrade drastically (I didnt even have an index to begin with).

Post #959339
Posted Tuesday, July 27, 2010 7:14 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, September 27, 2010 7:51 AM
Points: 15, Visits: 109
I personally have used the ranking functions quite a bit lately not to just remove duplicates but to be able to qualify what is removed. In one case, I needed to qualify one field based on the number of characters in a column and look at another column that may have one of two choices in it - "Reject" or "Reschedule". In this case, Reject weighed more than Reschedule. The ranking of both of the columns was added together to come up with the total ranking. Then all of the rows that were not the highest rank [based on a single column] were removed.
Post #959366
Posted Tuesday, July 27, 2010 7:44 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, November 25, 2014 6:16 PM
Points: 16, Visits: 40
The approach is/was unique and creative, and for that, it was an excellent article. But considering there are at least 2 other well known approaches to this problem, I feel that the article was missing the performance comparison that not only compared with the other approaches, but alsoo compared all approaches on a multi-column approach. Perhaps this was an excellent article as it left me, the reader, wanting lots more


Post #959393
Posted Tuesday, July 27, 2010 9:06 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, December 16, 2013 10:42 AM
Points: 96, Visits: 434
I find the RANK function to be much better used in situations where I might not be able to group by all fields in the select statement. Another way I have used it before is to find not the latest record, but the SECOND latest record by choosing where RANK = 2.

-------------------------------------------------------------------------------------------------
My SQL Server Blog
Post #959465
Posted Tuesday, July 27, 2010 9:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, October 20, 2011 10:58 PM
Points: 21, Visits: 44
Don't get me wrong. This is a good example of "how to use RANK() and ROW_NUMBER() functions". My problem is why would I do this instead of using DISTINCT?

There is already an article on "Ranking Functions" here...

[url=http://www.sqlservercentral.com/articles/T-SQL/69717/][/url]

Also, I know that DISTINCT is not the most efficient way to "de-duplicate" (nice phrase) over large datasets. Now if we are saying using Ranking Function approach is faster on larger data sets or largely skewed data sets, i.e. each row is already distinct, then I have learnt something. Else all I have learnt is (yet) one more way to write (potentially confusing) code. Using DISTINCT on the example cited in the article would be obvious. That's important where I come from.

So, if someone can please educate me as to when and why I would use the Ranking function method to get distinct records I would very much appreciate it.
Post #959510
Posted Tuesday, July 27, 2010 10:01 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 12:12 PM
Points: 1,945, Visits: 3,180
Nice article, but using a one-column table makes people say "Gee, I could do this with SELECT DISTINCT!" You should have made a case for speed (if any) and the ability to pick entire rows based on matching to a subset of columns. I would also add a "COUNT(*) OVER (PARTITION BY <column list>) AS dup_cnt" to the CTE to give the user some idea of what the data is like

Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #959520
Posted Tuesday, July 27, 2010 10:20 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 1:02 PM
Points: 120, Visits: 296
Say for example, if you have type 2 updates and you just wanted to know that which records are the latest in your table, you could do so by using Ranking functions (partitioned over time stamp obviously). If you would have done a distinct on the same table, there is no way to be sure if the record that your distinct query is picking up is indeed the latest one.

A very practical example for using ranking functions is the Database Reconciliation process.
Post #959534
Posted Tuesday, July 27, 2010 12:27 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, October 3, 2014 7:54 AM
Points: 116, Visits: 99
Anyone actually check this code?

I believe the RANK statement in the CTE is incorrect (see corrected version below) if the intent is to remove duplicates for ItemNumber only. At the very least, to return the dataset the author provides in the article:

777 10.10
888 13.13
999 16.16

Of course, I have to agree with others on this, for this particular example, GROUP with MIN would achieve the same result much more easily.

with GetMissingItems(Rank, RowNumber, ItemNumber, UnitCost) as (
select
--RANK() over (order by ItemNumber, UnitCost) as Rank
RANK() over (order by ItemNumber) as Rank
, ROW_NUMBER() over (order by ItemNumber, UnitCost) as RowNumber
, ItemNumber
, UnitCost
from MissingItems
)
select
ItemNumber
, UnitCost
from GetMissingItems
where Rank = RowNumber

select
ItemNumber
, min(UnitCost) 'UnitCost'
from #MissingItems
group by ItemNumber

Post #959619
Posted Tuesday, July 27, 2010 12:29 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, November 7, 2014 9:15 PM
Points: 15, Visits: 124
In most of the cases you need to retain the latest record from the duplicates. In that case matching rank=rownumber will not work.
So using ROW_NUMBER() partition and ORDER BY col DESC is always a good approach.
Post #959621
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse