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

How to use row_number() to filter duplicates with multiple joins?

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

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

有什么想法吗?

抱歉,还不能添加评论...

假设您的联接是正确的,为什么不直接使用 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,...  

从句法上看,您的查询似乎是正确的。但是,在没有任何 where 子句的情况下连接 4 个表时,您可能会尝试检索太多数据。可以改进查询的一些简单的事情是:

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

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

    中的数据
  • 如果这些仍然没有帮助,您将不得不开始研究索引。

    Select r1, r2, r3,rb1  
    From (
      Select * -- consider specifying only the columns that you need. Pretty sure you don't need every column from all 4 tables.
      , ROW_NUMBER() OVER (PARTITION BY r1 order by r6 DESC) AS rownbr  
      From table1 as t1
        join table2 as t2 ON t1.r2 = t2.rb1  
        Join table3 as t3 ON t3.r6 = t2.rc1  
        Join table4 as t4 ON t4.rx = t3.rdx
        where --may be something like date
        t1.createdate > '01-01-2016'
    ) source  
    Where  source.rownbr=1

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

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;