我有两个用于购买和用户的表(将包括下面的模式)。我想知道的是一段时间内所有用户的平均购买率。
说清楚点,我不想(总购买量/总用户数),我想知道更多像用户“每月2次购买”
我无法找出正确的算法来使用,虽然我认为这可能取决于购买之间的日期差异。任何关于如何思考这个算法和实际代码的帮助都将受到欢迎
用户表:
ID NAME EMAIL CREATED
2907 Philip xxxx Friday, April 6, 2018 14:55 PM
2859 Jane xxxx Friday, April 6, 2018 12:36 PM
采购表:
ID TIMESTAMP SKU USER_ID
1152497 Tuesday, April 10, 2018 12:05 AM 46 98271
1152498 Tuesday, April 10, 2018 12:05 AM 7887 207120
获取“用户id、购买次数和购买金额”的当前代码
SELECT count(*) AS count, sum(b.price) AS sum, u.id AS id, DATEDIFF(MAX(p.timestamp),MIN(p.timestamp)) as date_diff
FROM purchase p
LEFT JOIN book b on p.book_id = b.id
LEFT JOIN user u on p.user_id = u.id
WHERE date(p.timestamp) BETWEEN date(date_add(now(), INTERVAL -365 day)) AND date(date_add(now(), INTERVAL -1 day))
AND u.timestamp BETWEEN date(date_add(now(), INTERVAL -730 day)) AND date(date_add(now(), INTERVAL -365 day))
GROUP BY u.id
ORDER BY count DESC
当前结果
Count Sum ID Date Diff
634 4,240 88,981 226
549 9,432 130,140 337
387 518,009 113,849 320
暂无答案!
目前还没有任何答案,快来回答吧!