我想在过去30天的销售中添加更多权重(1.5)的最近30天销售和30至90天的销售

时间:2019-02-28 11:40:12

标签: sql amazon-athena presto

我想在过去30天的销售中增加最近30天销售和30至90天的销售,并增加权重(1.5)。

SELECT userid, SUM(premiumdetails_netpremium)
from policydetail,
     (SELECT userid AS partner_id, SUM(premiumdetails_netpremium) AS last30sale
      FROM policydetail AS pd
      WHERE DATE(sales_date) BETWEEN current_date - interval '30' day AND current_date
      group by userid
     ) AS A 
WHERE DATE(sales_date) BETWEEN current_date - interval '90' day AND current_date
group by userid 

1 个答案:

答案 0 :(得分:1)

我认为您需要条件聚合:

SELECT userid,
       SUM(CASE WHEN sales_date >= current_date - interval '30 day THEN 1.5 * premiumdetails_netpremium
                ELSE premiumdetails_netpremium
           END) as weighted_sum
FROM policydetail
WHERE sales_date >= current_date - interval '90' day 
GROUP BY userid ;

请注意,这是假设sales_date不在将来。