水善利万物而不争,处众人之所恶,故几于道
从订单信息表(order_info)中统计每个用户截止其每个下单日期的累积消费金额,以及每个用户在其每个下单日期的VIP等级。
用户vip等级根据累积消费金额计算,计算规则如下:
设累积消费总额为X,
若0=
若30000<=X<50000,则vip等级为白银会员
若50000<=X<80000,则vip为黄金会员
若80000<=X<100000,则vip等级为白金会员
若X>=100000,则vip等级为钻石会员
select
t2.user_id,
t2.create_date,
t2.sum_so_far,
case
when t2.sum_so_far >= 0 and t2.sum_so_far < 10000 then "普通会员"
when t2.sum_so_far < 30000 then "青铜会员"
when t2.sum_so_far < 50000 then "白银会员"
when t2.sum_so_far < 80000 then "黄金会员"
when t2.sum_so_far < 100000 then "白金会员"
else "钻石会员"
end vip_level
from
(
select
t1.user_id,
t1.create_date,
sum(t1.sum_amount) over(partition by user_id order by create_date rows between unbounded preceding and current row) sum_so_far
from
(
select
user_id,
create_date,
sum(total_amount) sum_amount
from
order_info
group by
user_id,
create_date
) t1
) t2
1.可能会出现这种情况,所以要以user_id和create_date分组,然后求sum(total_amount)
,计算出没人每天的总消费金额
2.然后求截至每个下单日期的累计下单金额sum(sum_amount),求和的范围用窗口函数限定,user_id分组,组内以create_date升序排序,然后行的范围是第一行(unbounded preceding
)到当前行(current row
)
3.最后在外层查询中用case when
分类