我会尝试旋转数据。这是一个 Denodo 社区站点的链接,其中显示了如何执行此操作:
https://community.denodo.com/kb/view/document/How%20to%20Pivot%20and%20Unpivot%20views?category=Combining+Data
针对您的具体情况,我创建了一个小型 Excel 数据源来在我命名为“p_sample”的视图中模拟您的问题(使用简化的日期和状态名称):
标识 |状态 | create_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 的每个状态日期,以及提交和批准之间的时间。
Query Results
唯一的缺点是如果状态代码列表非常大或控制不好,那么这个解决方案将不够灵活,但你的例子似乎表明这不会是一个问题。