需要在分区中选择最新的5条记录

use*_*666 5 sql teradata

我被困在一个要求中.这可能很简单,但我没有通过.

我有一个审计表Audit_Info,它捕获所有表的审计信息.表可以在同一个工作日期多次运行.我的要求是获取最近5个月的每个月的最新业务日期记录.可能会发生一个特定月份的表未运行.

表就像

table_name business_date src_rec tgt_rec del_rec load_timestamp
abc          25/10/2015   10      10      0       23/01/2016 03:06:56
abc          25/10/2015   10      10      0       23/01/2016 05:23:34
abc          07/09/2015   10      10      0       23/10/2015 05:37:30
abc          05/08/2015   10      10      0       23/09/2015 05:23:34
abc          15/06/2015   10      10      0       23/07/2015 05:23:34
abc          25/04/2015   10      10      0       23/05/2015 05:23:34
Run Code Online (Sandbox Code Playgroud)

类似的还有其他表格.我需要5张桌子.

谢谢你的帮助.

此致,阿米特 请看突出显示

dno*_*eth 3

根据您的预期结果,这应该接近:

select * from tab
where  -- last five months
   business_date >= add_months(trunc(current_date),-5)
qualify
   row_number()  
   over (partition by trunc(business_date)  -- every month
         order by business_date desc, load_timestamp desc) -- latest date
Run Code Online (Sandbox Code Playgroud)