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
andrew.riley
andrew.riley
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: 184
Comments posted to this topic are about the item Using Ranking Functions to Deduplicate Data
scottm30
scottm30
Say Hey Kid
Say Hey Kid (673 reputation)Say Hey Kid (673 reputation)Say Hey Kid (673 reputation)Say Hey Kid (673 reputation)Say Hey Kid (673 reputation)Say Hey Kid (673 reputation)Say Hey Kid (673 reputation)Say Hey Kid (673 reputation)

Group: General Forum Members
Points: 673 Visits: 383
Nice article, thanks for sharing.
The same thing can be achieved with just the row_number function as well.



select AlphaKey
from (
   select row_number() over(partition by AlphaKey order by AlphaKey) rownum,
   AlphaKey
   from @AlphaList
) al
where rownum = 1   
order by 1;



ziangij
ziangij
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2907 Visits: 374
yep, i too use this query to remove duplicates...

was wondering, which query is faster, this one or the one mentioned in the article...

scottm30 (7/26/2010)
Nice article, thanks for sharing.
The same thing can be achieved with just the row_number function as well.



select AlphaKey
from (
   select row_number() over(partition by AlphaKey order by AlphaKey) rownum,
   AlphaKey
   from @AlphaList
) al
where rownum = 1   
order by 1;



david.skov
david.skov
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 100
with the A,B,C,D,E example, the select distinct syntax would be better imho.
phil.wood 94423
phil.wood 94423
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 65
How about good ol' grouping to remove duplicates and supply any of the minimum, maximum, sum, count, average, etc. etc?

select ItemNumber, min(UnitCost) as MinCost from MissingItems
group by ItemNumber;

ItemNumber   MinCost
777   10.10
888   13.13
999   16.16
philip.cullingworth
philip.cullingworth
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1463 Visits: 967
I was interested to see what the difference in performance between these queries as I would always have used Row_Number with a partition before.

On the Northwind database (SQL 2005) I ran the following 2 queries

select
   a.ProductID
   ,a.UnitPrice
from
   (select
      ProductID
      ,UnitPrice
      ,Rank() over (order by ProductID) as Ranking
      ,Row_Number() over (order by ProductID,UnitPrice) as RowNumber
   from
      [Order Details]
   Wink as a
where
   a.Ranking=a.RowNumber
order by
   a.ProductID


select
   a.ProductID
   ,a.UnitPrice
from
   (select
      ProductID
      ,UnitPrice
      ,Row_Number() over (partition by ProductID order by UnitPrice) as RowNumber
   from
      [Order Details]
   Wink as a
where
   a.RowNumber=1
order by
   a.ProductID

In the Actual Execution Plan, the first query showed up as 64% and the second as 36%.

Far from exhaustive, but I'll probably stick to Row_Number with Partition by.

In (partial) answer to Phil Wood, if all I wanted was the lowest price for each product then I would use grouping, I'd probably use this method if I wanted to find the customer or order date as well, changing the code to

select
   a.ProductID
   ,a.UnitPrice
   ,b.CustomerID
   ,b.OrderDate
from
   (select
      ProductID
      ,UnitPrice
      ,OrderID
      ,Row_Number() over (partition by ProductID order by UnitPrice) as RowNumber
   from
      [Order Details]
   Wink as a
      inner join
   Orders as b
      on
         a.OrderID=b.OrderID
where
   a.RowNumber=1
order by
   a.ProductID

aszendi
aszendi
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 7
I agree. The simplest way to deduplicate is using grouping and aggregate functions as needed.
regards
Áron
Bhavesh-1094084
Bhavesh-1094084
SSC-Enthusiastic
SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)

Group: General Forum Members
Points: 149 Visits: 110
I had to remove unitprice from the orderby to get deduplicated results.


declare @TempTable table (itemnumber int, unitprice decimal(18, 2));

insert into @TempTable values
(777, 10.10),
(777, 11.11),
(777, 12.12),
(888, 13.13),
(888, 14.14),
(888, 15.15),
(999, 16.16),
(999, 17.17),
(999, 18.18);

with GetMissingItems(Rank, RowNumber, ItemNumber, UnitCost) as
(
   select
      RANK() over (order by ItemNumber) as Rank,
      ROW_NUMBER() over (order by ItemNumber) as RowNumber,
      itemnumber,
      unitprice
   from @TempTable      
) select * from GetMissingItems
where Rank = RowNumber;



Bhavesh
.NET and SQL Server Blog
messineo
messineo
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 4
In the example you provided you used one column. Say I have 4 columns and I want to deduplicate based on those. In other words I want to rank against those columns -- how would the SQL statements change?
philip.cullingworth
philip.cullingworth
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1463 Visits: 967
messineo,

If you have multiple columns that you want to deduplicate on you will need to include all of them in whichever method you use.

Here is some sample code using each method that should work (all based on the Northwind database). Note that in all these cases the data will be de-duplicated but which row is selected for each set of duplicates can not be predicted (so if you want the first order for each set you will need different code).


select
   a.ProductID
   ,a.UnitPrice
   ,a.Quantity
   ,a.Discount
from
   (select
      ProductID
      ,UnitPrice
      ,Quantity
      ,Discount
      ,Rank() over (order by ProductID,UnitPrice,Quantity,Discount) as Ranking
      ,Row_Number() over (order by ProductID,UnitPrice,Quantity,Discount) as RowNumber
   from
      [Order Details]
   Wink as a
where
   a.Ranking=a.RowNumber


select
   a.ProductID
   ,a.UnitPrice
   ,a.Quantity
   ,a.Discount
from
   (select
      ProductID
      ,UnitPrice
      ,Quantity
      ,Discount
      ,Row_Number() over (partition by ProductID,UnitPrice,Quantity,Discount order by ProductID) as RowNumber
   from
      [Order Details]
   Wink as a
where
   a.RowNumber=1


select
   ProductID
   ,UnitPrice
   ,Quantity
   ,Discount
from
   [Order Details]
group by
   ProductID
   ,UnitPrice
   ,Quantity
   ,Discount

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