如何在 teradata 中应用排名

how to apply ranking in teradata

我目前正在处理多个 table。 table1 要用数据库查找。例如。我已经处理 excel sheet 并更新了数据库中的记录,现在需要使用数据库查找记录是否已正确更新以及记录何时更新。 所以我有 table 1,其中包含我的数据,这些数据已经过处理并可以通过数据库查找。 基本上,我联合数据库中的所有记录并应用排名,然后使用我的 table1 进行查找。 记录可能已经更改了很多次,我需要最新的日期,所以我申请了排名。你能帮忙改正这个吗?因为我无法获得想要的结果

select a.*, b.update_dt||' '||c.udpate_dt|| as update_date
from table1 a
left join
 ( select distinct customer_id, max(last_updated_dt) as update_dt
   qualify rank() over(partition by customer_id order by Last_updated_dt) as rank
   from table2
   group by customer_id
 ) b
on a.customer_id=b.customer_id

left join
 ( select distinct customer_id, max(last_updated_dt) as update_dt
   qualify rank() over(partition by customer_id order by last_updated_dt) as rank
   from table3
   group by customer_id
 )c
on a.customer_id=c.customer_id

您需要 max(last_updated_dt)QUALIFY,但不能同时需要:

select a.*, b.update_dt||' '||c.udpate_dt|| as update_date
from table1 a
left join
 ( select customer_id, max(last_updated_dt) as update_dt
   from table2
   group by customer_id
 ) b
on a.customer_id=b.customer_id

left join
 ( select customer_id, max(last_updated_dt) as update_dt
   from table3
   group by customer_id
 )c
on a.customer_id=c.customer_id

如果您需要除最大日期之外的其他列:

select a.*, b.update_dt||' '||c.udpate_dt|| as update_date
from table1 a
left join
 ( select customer_id, last_updated_dt as update_dt, ...
   from table2
   qualify row_number() over(partition by customer_id 
                       order by last_updated_dt DESC) = 1
 ) b
on a.customer_id=b.customer_id

left join
 ( select customer_id, last_updated_dt as update_dt, ...
   from table3
   qualify row_number() over(partition by customer_id 
                       order by last_updated_dt DESC) = 1
 )c
on a.customer_id=c.customer_id