情况何时...然后..在哪里

时间:2018-02-05 15:34:38

标签: sql amazon-redshift

我有以下代码:

case when status=status2 and rank=5 then datediff(day, rep_onboard_date, client_signup_date) end as time_to_status2

其中:

rank= row_number() over(partition by rep_id order by client_signup_date)

status2= case when count(client_signup_date) over (partition by rep_id) >=5

这需要他第五个客户的rep_onboard_dateclient_signup_date之间的时差。

这样可以正常工作,但只填充每个代表的第5行,其余的是(null)。

我想要的是,如果为time_to_status的代表填充了任何行,那么所有行都应该具有相同的值。

简化查询:

with cte as (
select rep_id, rep_onboard_date, user_id, client_signup_date, /* a bunch of other fields,*/
   count(client_signup_date) over (partition by rep_id) as total_applicants,
case when count(client_signup_date) over (partition by rep_id) >=10 then 'status1'
        when count(client_signup_date) over (partition by rep_id) >=5 then 'status2'
        when count(client_signup_date) over (partition by rep_id) >=1 then 'status3'
        else 'none' end status,
      row_number() over(partition by rep_id order by client_signup_date) as rank,
    from table1 r
    left join table2 u on r.user_id=u.user_id
    left join table3 pi on u.user_id=pi.user_id
    )

    select *, 
     case when status='status1' and rank=10 then datediff(day, advisor_onboard_date, client_signup_date) end as time_to_status1,
     case when status='status2' and rank=5 then datediff(day, advisor_onboard_date, client_signup_date) end as time_to_status2,
     case when status='status3' and rank=1 then datediff(day, advisor_onboard_date, client_signup_date) end as time_to_status3
    from cte

当前输出:

rep_id user_id rep_onboard_date client_signup_date status   rank  time_to_status
1      1       1/1/2018         1/5/2018           status2  1     (null)
1      2       1/1/2018         1/5/2018           status2  2     (null)
1      3       1/1/2018         1/6/2018           status2  3     (null)
1      4       1/1/2018         1/7/2018           status2  4     (null)
1      5       1/1/2018         1/10/2018          status2  5     9
1      6       1/1/2018         1/15/2018          status2  6     (null)

预期产出:

rep_id user_id rep_onboard_date client_signup_date status   rank  time_to_status
1      1       1/1/2018          1/5/2018          status2  1     9
1      2       1/1/2018          1/5/2018          status2  2     9
1      3       1/1/2018          1/6/2018          status2  3     9
1      4       1/1/2018          1/7/2018          status2  4     9
1      5       1/1/2018          1/10/2018         status2  5     9
1      6       1/1/2018          1/15/2018         status2  6     9

2 个答案:

答案 0 :(得分:1)

似乎你不想要CASE,你想要一个子查询。像这样:

SELECT col1,
       col2,
       (SELECT datediff(day, rep_onboard_date, client_signup_date)
        FROM yourTable t2
        WHERE t2.rep_id = t.rep_id
        AND ((t.rank = 10  AND t.status = 'status')
        OR (t.rank = 5 AND t.status = 'status2')
        OR (t.rank = 1 AND t.status = 'status3'))) as time_to_status
FROM (yourTable or derivedTable with rank function) t

答案 1 :(得分:1)

我相信你想要的是窗口功能:

 select cte.*, 
        max(case when status = 'status1' and rank = 10
                 then datediff(day, advisor_onboard_date, client_signup_date)
            end) over (partition by rep_id) as time_to_status1
from cte;