[英]SQL Query for data moved from No-SQL Database
我有一个具有以下属性的 No-SQL 数据库。 我打算将其移至 SQL,因为它主要用于 OLAP。
我可以在这里执行以下查询吗? 从State转移到UnderReview所花费的时间。问题是客户可以有多个具有相同 state 的条目(版本会有所不同)。对于每个客户,我想要 Z99938282F04071859941E18 的时间。
对于这张表,答案是:
(T3 - T1 + T7 - T6)/2
任何指针将不胜感激。
客户ID | State | 版本 | 时间 | |
---|---|---|---|---|
C1 | 发起 | 1 | T1 | |
C1 | 审查不足 | 2 | T2 | |
C1 | 审查不足 | 3 | T3 | |
C1 | 完全的 | 4 | T4 | |
C2 | 发起 | 1 | T6 | |
C2 | 审查不足 | 2 | T7 |
您可以在customerid
中使用过滤器进行聚合,以获得最多一个Initiated
和一个UnderReview
。 是否要在计算中使用第一个值或最后一个值取决于您。 调整min()
或max()
以满足您的要求。
之后, avg()
将为您执行计算。
with prog as (
select customerid,
min(time) filter (where state = 'Initiated') as first_init,
max(time) filter (where state = 'UnderReview') as last_review
from your_table
group by customerid
)
select avg(last_review - first_init)
from prog
where last_review is not null
and first_init is not null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.