sqlamazon-redshiftaginity

New Customer Orders, but only if they purchased 1 week after registering


So what I'm trying to do is understand what products my new customers purchase, 1 week after they registered.

Currently, this is my query:

SELECT
    item_description_1,
    t3.new_division,
    quantity_ordered
FROM 
    mdwh.us_raw.order_report
    INNER JOIN l_act_account t2 ON customer_id = SUBSTRING(t2.channel_uid,4,18)
    FULL OUTER JOIN item_master_zs t3 ON SUBSTRING(upc,1,6) = t3.item_code
WHERE 
    quantity_ordered > 0
    AND customer_id IS NOT NULL
    AND customer_id != ('')
    AND customer_id LIKE 'US%'
    AND oms_order_date IS NOT NULL
    AND extended_amount < 1000 --NO BULK ORDERS
    AND UPPER(line_status) NOT IN ('','RETURN', 'CANCELLED')
    AND (quantity_ordered * unit_price_amount) > 0
    AND oms_order_date BETWEEN '2019-03-01' AND '2019-03-31'
    AND t2.create_timestamp BETWEEN '2019-03-01' AND '2019-03-31'
    AND SUBSTRING(upc,1,6) IN (SELECT item_code FROM item_master_zs WHERE new_division BETWEEN '11' AND '39') --Overall
GROUP BY
    item_description_1,
    t3.new_division,
    quantity_ordered
ORDER BY
    quantity_ordered desc
LIMIT 60

This query is giving me the top 60 items purchased within March, from new customers I gained in March. Here's what the output looks like right now:

enter image description here

All I need now is an output that looks exactly like the above, but only showing me the items purchased 7 days AFTER their create_timestamp date (when they registered).

The point of this is as follows: some new customers may register an account and immediately make a purchase because of some sort of coupon or perhaps a referral. However, if I can see which products users purchased (where they seem to have waited a week before making a purchase), then I can see which products triggered them to come and make that purchase.

Please let me know if I made anything unclear, or if you need additional info. Any help would be greatly appreciated!!


Solution

  • It appears you only want to include orders where the order date (oms_order_date) is within 7 days after registration date (create_timestamp).

    Assuming that the order date is a DATE and the registration is a TIMESTAMP, you can only work in full days, not down to the hour.

    Therefore, you'd use:

    WHERE oms_order_date - create_timestamp <= INTERVAL '7 days'