首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >时间序列“状态”计算

时间序列“状态”计算
EN

Stack Overflow用户
提问于 2018-03-19 13:51:15
回答 1查看 41关注 0票数 1

样本数据:

代码语言:javascript
复制
rep_signup_date rep_id client_registration_date client_id 
1/2/2018        1      1/5/2018                 1          
1/2/2018        1      1/9/2018                 2
1/2/2018        1      2/15/2018                3
1/4/2018        2      2/3/2018                 4
1/4/2018        2      3/9/2018                 5
2/1/2018        3      2/2/2018                 6

我们对rep“状态”进行分类的方式是基于#客户端:1客户端-状态1,2客户端-状态2,3+客户端-状态3,因此在当前日期我们知道以下信息:

代码语言:javascript
复制
select rep_signup_date, rep_id,  
case when count(client_id) over (partition by rep_id) >=3 then '3'
     when count(client_id)  over (partition by rep_id) =2 then '2'
     when count(client_id)  over (partition by rep_id) =1 then '1'
     end status
from reps r
left join clients c on c.rep_id=r.id

rep_signup_date rep_id  status
1/2/2018        1       3     
1/4/2018        2       2
2/1/2018        3       1

但是,这些状态是从当前日期开始的;我尝试为月份添加date_trunc('month', client_registration_date)::date,但它仍然根据最大日期(而不是静态时间点)将数据作为当前快照。

我想要做的是在每个月底获得状态-例如,rep 1在1月底是状态2。

预期输出:

代码语言:javascript
复制
rep_signup_date rep_id month    status
1/2/2018        1      1/1/2018 2
1/2/2018        1      2/1/2018 3     
1/4/2018        2      2/1/2018 1
1/4/2018        2      3/1/2018 2
2/1/2018        3      2/1/2018 1

我该怎样走?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-19 13:53:45

使用order by

代码语言:javascript
复制
select rep_signup_date, rep_id,  
       (case when count(client_id) over (partition by rep_id order by client_registration_date rows between unbounded preceding and current row) >= 3 then '3'
             when count(client_id) over (partition by rep_id order by client_registration_date rows between unbounded preceding and current row) = 2 then '2'
             when count(client_id) over (partition by rep_id order by client_registration_date rows between unbounded preceding and current row) = 1 then '1'
        end) as status
from reps r left join
     clients c
     on c.rep_id = r.id;

您似乎每个客户端/rep有一行,所以使用row_number()比使用累积计数要简单得多:

代码语言:javascript
复制
select rep_signup_date, rep_id,  
       (case when row_number() over (partition by rep_id order by client_registration_date ) >= 3 then '3'
             when row_number() over (partition by rep_id order by client_registration_date rows) = 2 then '2'
             when row_number() over (partition by rep_id order by client_registration_date = 1 then '1'
        end) as status
from reps r left join
     clients c
     on c.rep_id = r.id;

这可以进一步简化为:

代码语言:javascript
复制
select rep_signup_date, rep_id,  
       (case row_number() over (partition by rep_id order by client_registration_date ) >= 3
             when 1 then '1'
             when 2 then '2'
             else '3'
        end) as status
from reps r left join
     clients c
     on c.rep_id = r.id;

甚至:

代码语言:javascript
复制
select rep_signup_date, rep_id,  
       greatest(row_number() over (partition by rep_id order by client_registration_date ), 3) as status
from reps r left join
     clients c
     on c.rep_id = r.id;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49364629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档