如果状态不时更改(历史数据),我如何查看正确的状态编号
考虑到约会的状态可以更改或无法更改,我想获取在特定时间内设置的确切约会数。谢谢!
WITH historisation AS (SELECT created_by_name AS "Agent", t1.lead_id, t1.date_created, CASE WHEN t1.status_label='Appointment Set' THEN 1 ELSE 0 END AS "Appointments Set"
FROM leads__opportunities t1
INNER JOIN (SELECT lead_id, MAX(date_created), date_updated FROM leads__opportunities GROUP BY 1,3) t2
ON t1.lead_id=t2.lead_id AND t1.date_created=t2.date_updated
WHERE t1.status_label='Appointment Set'
GROUP BY 1,2,3,4)
SELECT "Agent", SUM("Appointments Set") AS "Total Appointments Set"
FROM historisation
WHERE date_created>=now() - INTERVAL '1 Week'
GROUP BY 1