Sql 标记每个用户达到的金额阈值

Sql 标记每个用户达到的金额阈值,sql,sql-server,Sql,Sql Server,故事: 两个条件: 我需要计算用户花费的时间 20美元、50美元和100美元,以其注册日期为准 如果某人的首次购买价格>100美元,则 3个阈值的天数相同 我能够得到标记阈值的逻辑,但我被用户的第一次购买超过一个或两个阈值所困扰 目标: 我需要计算它们达到20美元、50美元和100美元所需的天数 当前查询: 我可能不得不改变整个逻辑来满足第二个条件,但是的,我被卡住了。以下代码正确地标记正在达到的阈值 select user_id, register_date ,total

故事: 两个条件:

  • 我需要计算用户花费的时间 20美元、50美元和100美元,以其注册日期为准
  • 如果某人的首次购买价格>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')
    

    只需使用条件聚合和
    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;