0

I was wondering if I could get some help in terms of calculating a difference between the date of various statuses. I have a view with column named “id”, “create_dt” and “status”. I would have various statuses like submit, approve, deliver etc. Now, I want to find the time it took for a specific id between Approve and Submit status. What I am thinking currently is, creating few additional fields based on the status (I can use case statement for that) and finding the difference in time between the newly created date columns for various statuses. The problem here is, I am not sure how to drive the calculation based on the specific id. I can’t do lag or lead because some “id” might go through different statuses and some might not (it’s not consistent). I can’t create a the new date column based on id (something like partition by) because I am using case statement. Could someone point me to the right direction?

Below is the screenshot of how my data currently looks like (using the case statement) and what my desired output is. Current Result

Expected Result

From the expected result, I could easily find the difference between submitted and approved status for any ID using case statement whereas for the current result, I am not able to.

Thank you,

4

1 回答 1

2

我会尝试旋转数据。这是一个 Denodo 社区站点的链接,它显示了如何执行此操作:

https://community.denodo.com/kb/view/document/How%20to%20Pivot%20and%20Unpivot%20views?category=Combining+Data

对于您的具体情况,我创建了一个小型 excel 数据源来在我命名为“p_sample”的视图中模拟您的问题(使用简化的日期和状态名称):

编号 | 状态 | 创建_dt
1 | 提交 | 2017
年 1 月 1 日 1 | 批准 | 2017
年 2 月 1 日 1 | 交付 | 2017 年 2 月
2 日 2 | 提交 | 2017年 1 月
1 日 2 | 批准 | 2017 年 1 月 10
日 2 | 交付 | 2017年 2 月
1 日 3 | 提交 | 2017 年 1 月 1 日
....

由于 Denodo 似乎不支持 PIVOT 运算符,因此我们可以使用以下 VQL 来调整您的状态日期,以便它们都在同一行:

select id
  , max(case when status = 'submit' then create_dt end) as submit_dt
  , max(case when status = 'approve' then create_dt end) as approve_dt
  , max(case when status = 'deliver' then create_dt end) as deliver_dt
  , max(case when status = 'reject' then create_dt end) as reject_dt
  , max(case when status = 'other' then create_dt end) as other_dt
from p_sample
group by id

然后我们可以将该查询用作内联视图来执行日期数学运算(或者在 Denodo 中,您可以创建这 2 个视图 - 一个具有上述 VQL,然后在应用日期数学的视图之上选择一个视图):

select *, approve_dt - submit_dt as time_to_aprove
from (
select id
  , max(case when status = 'submit' then create_dt end) as submit_dt
  , max(case when status = 'approve' then create_dt end) as approve_dt
  , max(case when status = 'deliver' then create_dt end) as deliver_dt
  , max(case when status = 'reject' then create_dt end) as reject_dt
  , max(case when status = 'other' then create_dt end) as other_dt
from p_sample
group by id
) AS Pivot

运行此程序时,您将获得 ID 的每个状态日期,以及提交和批准之间的时间。

查询结果

唯一的缺点是如果状态代码列表非常大或控制不好,那么这个解决方案将不够灵活,但你的例子似乎表明这不会是一个问题。

于 2017-05-22T14:45:20.640 回答