在SQL中查找用户的平均购买率

时间:2018-04-13 13:40:56

标签: mysql

我有两个购买和用户的表(将包括下面的架构)。我想知道的是在一个时间范围内所有用户的平均购买率。

要明确,我不想要(总购买量/总用户数),我想了解更多用户每月购买 " 2次购买&#34 ;

我无法找出合适的算法,尽管我认为它可能依赖于购买之间的约会。欢迎任何有关如何考虑此算法和实际代码的帮助

用户表:

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

0 个答案:

没有答案