Sql 时间序列“;“地位”;计算

Sql 时间序列“;“地位”;计算,sql,amazon-redshift,Sql,Amazon Redshift,样本数据: 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/

样本数据:

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_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
我们对代表“状态”进行分类的方式是基于客户数量: 1个客户机-状态1,2个客户机-状态2,3+客户机-状态3,因此目前我们了解以下情况:

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\u trunc('month',client\u registration\u date)::date
,但它仍然根据最大日期提供当前快照数据,而不是静态时间点

我希望能够在每个月底获得状态-例如,1月底的RepID1是状态2

预期输出:

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_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

我怎么去那里?谢谢。

使用
订购人

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;
每个客户机/代表似乎有一行,因此使用
行数()
比使用累积计数要简单得多:

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;
这可以进一步简化为:

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;
甚至:

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;

谢谢,但是我添加了date\u trunc('month',client\u registration\u date)::date和输出似乎每天都有:排序为rep id=1,month=Jan,status=1,然后rep id=1,month=Jan,status=2;但是我只需要status=2记录(如果是按月记录的话)。非常感谢。样本数据和期望的结果将传达您想要的信息。@GordonLinoff两者都提供了,谢谢