Пометка количества порогов, достигнутых на пользователя - PullRequest
1 голос
/ 08 ноября 2019

История: Два условия:

  1. Мне нужно рассчитать количество дней, которое требуется пользователю, чтобы потратить более 20, 50 и 100 $, на основе его register_date
  2. Если чья-то первая покупка> 100 $, то количество дней будет одинаковым для 3 порогов.

Мне удалось получить логику для пометки порогов, но язастрял для первой покупки пользователей, которая пересекает один или два порога.

Цель: Мне нужно рассчитать количество дней, которое требуется им, чтобы достичь более 20, 50 и 100 $.

Текущий запрос: Возможно, мне придется изменить всю логику, чтобы удовлетворить второе условие, но да, я застрял. Следующий код правильно помечает достигаемые пороги.

select
    user_id, register_date
    ,total
    ,order_date
    ,cumulative_sum
    ,threshold
    ,case 
        when threshold+LAG(threshold,1,0) over (partition by user_id order by order_date)=1 then 1
        when threshold+LAG(threshold,1,0) over (partition by user_id order by order_date)=3 then 2
        when threshold+LAG(threshold,1,0) over (partition by user_id order by order_date)=5 then 3
        else 0 end as flag
from (
    select
        user_id, register_date
        ,total
        ,order_date
        ,cumulative_sum
        ,case 
            When cumulative_sum>=100 then 3 
            When cumulative_sum>=50 then 2 
            When cumulative_sum>=20 then 1 else 0 end as threshold
    from (
        select 
            user_id, register_date
            ,(price*quantity) as total
            ,order_date 
            ,SUM(price*quantity) over (partition by user_id order by order_date asc) as cumulative_sum
        from #t1
    ) as base1
) as base2

Данные:

CREATE TABLE #t1 (user_id int, price int, quantity int, order_date datetime,register_date datetime)
insert into #t1 values
(1,10,1,'2019-01-01 00:00:00.000','2019-01-01 00:00:00.000'),
(1,15,1,'2019-01-02 00:00:00.000','2019-01-01 00:00:00.000'),
(1,30,1,'2019-01-03 00:00:00.000','2019-01-01 00:00:00.000'),
(1,100,1,'2019-01-04 00:00:00.000','2019-01-01 00:00:00.000'),
(2,60,1,'2019-01-02 00:00:00.000','2019-01-01 00:00:00.000'),
(3,150,1,'2019-01-03 00:00:00.000','2019-01-01 00:00:00.000'),
(4,10,1,'2019-01-04 00:00:00.000','2019-01-01 00:00:00.000'),
(4,20,1,'2019-01-05 00:00:00.000','2019-01-01 00:00:00.000'),
(4,50,2,'2019-01-06 00:00:00.000','2019-01-01 00:00:00.000')

1 Ответ

1 голос
/ 08 ноября 2019

Просто используйте условное агрегирование и datediff():

select user_id,
       datediff(day, min(register_date),
                min(case when cumulative_sum >= 20 then order_date end)
               ) as days_to_20,
       datediff(day, min(register_date),
                min(case when cumulative_sum >= 50 then order_date end)
               ) as days_to_50,
       datediff(day, min(register_date),
                min(case when cumulative_sum >= 100 then order_date end)
               ) as days_to_100
from (select t.*,
             sum(price * quantity) over (partition by user_id order by order_date asc) as cumulative_sum
      from #t1 t
     ) t
group by user_id;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...