我正在使用Presto SQL并尝试计算按国家/地区和customer_status细分的每天登录网站的客户数量?将状态从“新”更新为“返回”需要2天。结果应如下所示:
date country status total_count
2019-08-01 usa new 1
return null
canada new 1
return 1
table a : country of customers
login_date id country
2019-08-01 1 usa
2019-08-01 4 canada
2019-08-01 5 canada
2019-08-02 1 usa
2019-08-02 3 usa
2019-08-02 4 usa
2019-08-03 1 usa
2019-08-03 2 canada
2019-08-03 3 usa
2019-08-03 4 usa
2019-08-03 5 canada
...
table b : daily status of customers.
**It takes 2 days to update the status from ‘new’ to ‘return’
purchase_date id customer_status
2019-08-01 5 return
2019-08-02 4 new
2019-08-02 5 return
2019-08-03 1 return
2019-08-03 2 new
2019-08-03 3 return
2019-08-04 4 return
...
关于如何联接两个具有不同日期的表的任何建议?我认为该表应在同一日期加入,但要延迟2天。
SELECT a.date,
a.country,
b.customer_status,
COUNT(id)
FROM table_a a
LEFT JOIN table_b b
ON (a.id = b.id AND ‘how should I join on date that’s 2 days delay?’)
GROUP BY 1,2
非常感谢您!
答案 0 :(得分:1)
您需要这样做,DATE_ADD(
日期, INTERVAL 2 DAY)
SELECT a.date,
a.country,
b.customer_status,
COUNT(id)
FROM table_a a
LEFT JOIN table_b b
ON (a.id = b.id AND a.login_date = DATE_ADD(b.purchase_date, INTERVAL 2 DAY))
GROUP BY 1,2