|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, June 05, 2013 12:03 PM
Points: 32,
Visits: 47
|
|
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.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 1:14 AM
Points: 120,
Visits: 282
|
|
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).
|
|
|
|
|
Grasshopper
      
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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Sunday, June 09, 2013 6:55 PM
Points: 16,
Visits: 38
|
|
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
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Thursday, September 20, 2012 5:21 PM
Points: 84,
Visits: 391
|
|
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
|
|
|
|
|
Grasshopper
      
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.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 1:14 AM
Points: 120,
Visits: 282
|
|
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.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, March 14, 2012 10:28 AM
Points: 116,
Visits: 73
|
|
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
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, May 30, 2013 10:50 AM
Points: 15,
Visits: 103
|
|
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.
|
|
|
|