0

我有一个表 - user_tracking - 存储 user_id、购买 sku 和事件 time_created。每次用户返回购买原始 user_id 时都会引用一个新的时间戳:

User_ID    Sku             Time_Created
  1       1234          2012-10-01 01:00:00
  2       2345          2012-10-02 02:00:00
  3       6789          2012-10-02 01:00:00
  2       5432          2012-10-04 04:00:00

我想衡量退货客户的使用情况,但仅适用于在初次购买后 7-60 天内退货的客户。目前我的查询看起来像:

SELECT
  total_purchases.user_id             as user_1_id,
  total_purchases.time_created        as time_1_created,
  total_purchases.total_purchases     as total_original_purchases,
  total_return.user_id                as user_2_id,
  total_return.time_created           as time_2_created,
  total_return.total_return_purchases as total_return_purchases
FROM (SELECT
    user_tracking.user_id                as user_id,
    user_tracking.time_created           as time_created,
    COUNT(DISTINCT user_tracking.sku)    as total_purchases
      FROM user_tracking
      WHERE user_tracking.time_created BETWEEN "2012-10-01 00:00:00"
      AND "2012-10-15 00:00:00") AS total_purchases
  LEFT JOIN (SELECT
           user_tracking.user_id                as user_id,
           user_tracking.time_created           as time_created,
           COUNT(DISTINCT user_tracking.sku)    as total_return_purchases
         FROM user_tracking
         WHERE user_tracking.time_created BETWEEN "2012-10-01 00:00:00"
         and "2012-12-15 00:00:00") AS total_return
    ON total_purchases.user_id = total_return.user_id

我怎样才能确保我只衡量原始用户在 7-60 天内的购买情况?

4

1 回答 1

0

您可以使用间隔

AND datecolumn BETWEEN (datecolumn, INTERVAL 7 DAYS) AND (datecolumn, INTERVAL 60 DAYS)
于 2013-01-29T05:37:20.673 回答