如何在teradata中应用排名

如何在teradata中应用排名,teradata,Teradata,我目前正在处理多个表。要使用数据库查找的表1。例如,我已经在excel工作表上工作并更新了数据库中的记录,现在需要使用数据库查找记录是否已正确更新以及记录何时更新。 因此,我有表1,其中包含我的数据,已被处理,并将与DB一起查看。 基本上,我联合数据库中的所有记录并应用排名,然后使用我的表1进行查找。 记录可能已经改变了很多次,我需要最新的日期,所以我申请排名。你能帮我纠正一下吗。因为我不能得到想要的结果 select a.*, b.update_dt||' '||c.udpate_dt|| a

我目前正在处理多个表。要使用数据库查找的表1。例如,我已经在excel工作表上工作并更新了数据库中的记录,现在需要使用数据库查找记录是否已正确更新以及记录何时更新。 因此,我有表1,其中包含我的数据,已被处理,并将与DB一起查看。 基本上,我联合数据库中的所有记录并应用排名,然后使用我的表1进行查找。 记录可能已经改变了很多次,我需要最新的日期,所以我申请排名。你能帮我纠正一下吗。因为我不能得到想要的结果

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(上次更新的\u dt)
限定
,但不能同时满足以下两个条件:

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
您需要
max(上次更新的\u dt)
限定
,但不能同时满足以下两个条件:

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

有点紧急,如果有人能帮上忙,对我来说会很好。非常感谢,如果有人能在这方面帮助我,我会很高兴的。谢谢谢谢,一个问题-就像交易被更新了三次,我需要最新的日期。排号可以吗?或Rank?@puneetmadan:
行号
保证分配唯一值(如果同一日期有多个更新,将随机选择一个),但
Rank
将为同一日期分配相同的值,从而返回上一日期的所有行(类似于
MAX
版本)。非常感谢,一个问题-就像一个交易被更新了三次,我需要最新的日期。排号可以吗?或Rank?@puneetmadan:
行编号
保证分配唯一的值(如果同一日期有多个更新,将随机选择一个更新),但
Rank
将为同一日期分配相同的值,从而返回上一日期的所有行(类似于
MAX
版本)。