SQL Clone
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
mross01
mross01
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 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.
rvphx
rvphx
SSC Veteran
SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)

Group: General Forum Members
Points: 242 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).
grc-80104
grc-80104
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 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.
Lambchop4697
Lambchop4697
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: 43
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 :-D



amenjonathan
amenjonathan
Old Hand
Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)

Group: General Forum Members
Points: 314 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
Pagan DBA
Pagan DBA
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 70
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.
rvphx
rvphx
SSC Veteran
SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)

Group: General Forum Members
Points: 242 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.
Brian Barkauskas
Brian Barkauskas
SSC-Enthusiastic
SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)

Group: General Forum Members
Points: 160 Visits: 179
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


Vamshi Bharath
Vamshi Bharath
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 143
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.
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