【问题标题】:Implement aggregation in Teradata在 Teradata 中实现聚合
【发布时间】:2018-08-16 21:47:57
【问题描述】:

我想按升序聚合 2 个字段 proct_dt、dw_job_id 通过使用以下查询和结果,我的情况会很清楚。

第一个查询:-

sel * from scratch.COGIPF_RUNREPORT_test1 order by proct_dt,dw_job_id where dw_job_id =10309

输出:-

dw_job_id   proct_dt           start_ts          end_ts                      time_diff  

1 10,309 2018-03-06 00:00:00 2018-03-06 07:04:18 2018-03-06 07:04:22.457000 0
2 10,309 2018-03-06 00:00:00 2018-03-06 06:58:50 2018-03-06 06:58:51.029000 0
3 10,309 2018-03-07 00:00:00 2018-03-07 06:35:36 2018-03-07 06:36:03.809000 1
4 10,309 2018-03-06 00:00:00 2018-03-06 07:00:35 2018-03-06 07:00:40.702000 0

5 10,309 2018-03-06 00:00:00 2018-03-06 06:30:25 2018-03-06 06:30:42.759000 0

6 10,309 2018-03-06 00:00:00 2018-03-06 07:10:27 2018-03-06 07:10:28.715000 0

7 10,309 2018-03-06 00:00:00 2018-03-06 06:59:44 2018-03-06 06:59:48.315000 0

8 10,309 2018-03-06 00:00:00 2018-03-06 07:00:15 2018-03-06 07:00:15.086000 0

9 10,309 2018-03-06 00:00:00 2018-03-06 07:04:02 2018-03-06 07:04:02.925000 0

第二次查询:-

sel * from scratch.fact_test order by proct_dt asc ,dw_job_id asc where dw_job_id =10309

结果:-

dw_job_id   proct_dt            start_ts              end_ts      status

1 10,309 2018-03-06 00:00:00 2018-03-06 06:30:25 2018-03-06 06:30:42.759 12

2 10,309 2018-03-07 00:00:00 2018-03-07 06:35:36 2018-03-07 06:36:03.809 12

所以在第二个查询中,我通过第一次出现 proct_dt,start_ts,end_ts 得到了第一个表的期望结果

请让我知道任何澄清 如果有人能帮助解决这个问题,那就太好了。

谢谢,

【问题讨论】:

    标签: group-by aggregate-functions teradata teradata-sql-assistant


    【解决方案1】:

    从您的解释中不清楚您想要什么,但看起来您希望每天完成第一次工作,这很容易使用 Row_Number:

    select * 
    from scratch.COGIPF_RUNREPORT_test1
    where dw_job_id =10309
    qualify
       row_number()
       over (partition by dw_job_id, proc_dt -- for each job & date
             order by start_ts) = 1          -- only the 1st run
    

    【讨论】:

    • @HimanshuPant:将 Row_Number 移动到 Select 列表中并在没有 Qualify 的情况下运行它,您将看到排名是如何分配的
    • 感谢 dnoeth 的所有帮助
    猜你喜欢
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    相关资源
    最近更新 更多