我想在过去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
答案 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
不在将来。