Mysql SQL从客户最新赢得的积分日期及其前一年获得积分的总和

Mysql SQL从客户最新赢得的积分日期及其前一年获得积分的总和,mysql,sql,Mysql,Sql,我需要获得每位客户的积分总和,但每位客户的积分总和是从他们上次赢得积分的日期开始计算的,并且从一年前开始计算 我有两层。一个级别适用于得分至少为1分的客户,另一个级别适用于得分至少为1000分的客户 我想获得积分>=950和

我需要获得每位客户的积分总和,但每位客户的积分总和是从他们上次赢得积分的日期开始计算的,并且从一年前开始计算

我有两层。一个级别适用于得分至少为1分的客户,另一个级别适用于得分至少为1000分的客户

我想获得积分>=950和<1000的客户的记录。我计划把他们转移到第二层作为奖励。我最近导入了客户,即使他们至少得了1分,他们也不会自动添加到第一层

这是一个滚动年类型的计算。vts.custom_start_date是计算积分的合格日期,表示分层活动开始的时间

滚动年计算是指如果你今天获得100分,它将检查你从今天开始的一年中获得的所有分数,看看你现在是否有资格进入一个等级

我需要在我的查询中添加一些内容,其中它计算从客户的最新赢得积分日期到该日期前一年赢得的积分之和

select
       c.id,
       c.email,
       sum(p.reward_points) as total_points,
       vt.name,
       cvt.tier_id

from vip_tiers_settings vts
left join customers c on c.merchant_id = vts.merchant_id
left join customers_vip_tiers cvt on c.id = cvt.customer_id
left join vip_tiers vt on vt.id=cvt.tier_id
left join perks p on p.customer_id = c.id
                        and p.created_at >= coalesce(vts.custom_start_date,0)
                        and p.completed = 1
                        and p.reversed = 0
                        and p.expired = 0

where
c.merchant_id = 50506
and c.id not in(select customer_id from customers_vip_tiers cvv where cvv.merchant_id=50506 and cvv.tier_id=1734)

group by c.id having sum(p.reward_points) >= '950' and sum(p.reward_points) < '1000';