简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM