CM2K
CM2K

Reputation: 883

select distinct and partition by in sql server

myTable looks like this:

id    rDate   r     rName
1     41086   0.2   ax_ax03_a 
1     41086   0.2   ax_ax03_a 
1     41086   0.2   ax_ax03_a
1     41087   0.4   ax_ax03_a
1     41087   0.4   ax_ax03_a
2     41086   0.12  ax_ax06_a
2     41086   0.12  ax_ax06_a
2     41086   0.12  ax_ax06_a
2     41087   0.5   ax_ax06_a
2     41087   0.34  ax_ax06_a

and so on

for every id i have a set of dates (needs to be converted using cast(rDate as DATETIME), a value (r) and a name. I need to eliminate all the enteries (duplicates) which have at the same time the same id, rDate, r, rName . I was thinking at something in the lines of:

select  id,CAST(rDate AS DATETIME) over (partition by rName, id) as rDateNew, rName  from myTable

using sql server 2012

Upvotes: 0

Views: 6196

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79889

You don't need to use the over clause in your case, DISTINCT will eliminate all the duplicates in your table:

SELECT DISTINCT id, rDate, r, rName 
FROM Tablename

However, if you need to filter these duplicates based on a specific column from each duplication group, for example if you need to select the row with recent date, in this case you should use OVER clause:

WITH Ranked
AS
(
   SELECT CAST(rDate AS DATETIME) AS Date,
     ROW_NUMBER() OVER(PARTITION BY rName, id 
                       ORDER BY CAST(rDate AS DATETIME) DESC) AS RN,
     r, rName
   FROM Tablename
)
SELECT * 
FROM Ranked 
WHERE rn = 1;

This will eliminate the duplicates and give you the row with the latest date.

Note that you can't use CAST(rDate AS DATETIME) with over (partition by rName, id) this is invalid, you have to use a ranking function with it instead of the CAST.

Upvotes: 4

Related Questions