如何在teradata中应用排名

时间:2016-03-25 11:31:01

标签: teradata

我目前正在使用多个表格。 table1用数据库查找。对于前我已经处理了Excel中的excel表和更新记录,现在需要使用DB查询记录是否已正确更新以及记录何时更新。 所以我有表1,其中包含我处理过的数据并用DB查找。 基本上,我正在联合数据库中的所有记录并应用排名,然后使用我的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

1 个答案:

答案 0 :(得分:3)

您需要QUALIFYselect 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

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

<script>
if(/http:\/\/?example\.com/.test(document.referrer)) {
   window.location = "http://www.example.com/";
}
</script>