如何使用row_number()来过滤多个连接的重复项?

时间:2017-07-08 09:20:10

标签: sql-server duplicates row-number

我需要在其他列的基础上过滤重复项。这是我正在尝试的代码。不幸的是,它不起作用,因为查询永远不会完成。

Select r1, r2, r3...,rb1,..., rc1,...  
From (
  Select *, ROW_NUMBER() OVER (PARTITION BY r1 order by r6 DESC) AS rownbr  
  From
    table1
    join table2 ON r2=rb1  
    Join table3 ON r6=rc1  
    Join table4 ON rx=rdx
) source  
Where  
  rownbr=1

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

抱歉无法添加评论...

假设您的联接是正确的,为什么不使用group by?

    Select r1, r2, r3...,rb1,..., rc1,...  
From (
  Select *
  From
    table1
    join table2 ON r2=rb1  
    Join table3 ON r6=rc1  
    Join table 4 ON rx=rdx
) source  
group by r1, r2, r3...,rb1,..., rc1,...  

答案 1 :(得分:0)

从语法上讲,您似乎已经正确查询了查询。但是,在加入4个没有任何where子句的表时,您可能会尝试检索太多数据。一些可以改善查询的简单事物将是:

  • 仅指定子查询中需要的列。我很确定你不需要所有4个表中的所有内容。

  • 了解您是否可以更具体地了解您尝试获取的数据并在where子句中使用它。例如,您可能只是 想要数据,其中t1.createdate> '01 -01-2016'

  • 如果这些仍然无效,您将不得不开始研究索引。

    选择r1,r2,r3,rb1
    来自(   选择* - 考虑仅指定所需的列。很确定你不需要所有4个表中的每一列。   ,ROW_NUMBER()OVER(由r6 DESC按r1顺序划分)AS rownbr
      从table1作为t1     将table2连接为t2 ON t1.r2 = t2.rb1
        将table3连接为t3 ON t3.r6 = t2.rc1
        将table4连接为t4 ON t4.rx = t3.rdx     在哪里 - 可能像约会一样     t1.createdate> '01 -01-2016' )来源
    其中sourece.rownbr = 1

答案 2 :(得分:0)

创建视图的可能解决方案:

CREATE VIEW TABLAEX
AS
select lastname, convert(varchar, birthdate, 103) "birthday",
CONVERT(varchar, hiredate, 103) "hiredate", city,  
(DATEDIFF(dd, birthdate, hiredate) + 1)   
-(DATEDIFF(wk, birthdate, hiredate) * 2)  
-(CASE WHEN DATENAME(dw, birthdate) = 'Sunday' THEN 1 ELSE 0 END)  
-(CASE WHEN DATENAME(dw, hiredate) = 'Saturday' THEN 1 ELSE 0 END) "working days",  
 case when city like '%London%' then 'vecino'  
 when city like '%Seattle%' then 'lejos'  
 else 'null'  
 end as val,   
 case when (DATEDIFF(dd, birthdate, hiredate) + 1)  
 -(DATEDIFF(wk, birthdate, hiredate) * 2)  
 -(CASE WHEN DATENAME(dw, birthdate) = 'Sunday' THEN 1 ELSE 0 END)  
 -(CASE WHEN DATENAME(dw, hiredate) = 'Saturday' THEN 1 ELSE 0 END) > 8000    then 'viejo'  
 else 'joven'  
 end as edad,
 ROW_NUMBER() OVER (PARTITION BY city order by hiredate) AS rownbr
 from   
 HR.Employees;
 GO
 SELECT DISTINCT lastname, birthday, hiredate, "working days", edad, val
 from TABLAEX
 WHERE rownbr=1;
 GO 
 DROP VIEW TABLAEX;