0

我想按升序聚合 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 从第一个表中得到了期望的结果

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

谢谢,

4

1 回答 1

0

从你的解释中不清楚你想要什么,但看起来你想要每天第一次运行作业,这很容易使用 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
于 2018-03-08T16:48:49.323 回答