I'm having trouble joining 2 tables that will allow me to see which new customers purchased at least 1 of a specific item. The first one pulls new customer accounts from a specific date range, and the second pulls customers that made at least on purchase of an item during a specific date range
select customer.id from customer c
inner join device d on u.id = d.user_id
where c.created_date between {{start}} and {{end}}
group by c.id
and
select customer_id from purchase_history
where item_id = {{itemID}} and created_date between {{starts}} and {{ends}} group by customer_id
I'm stuck with the join part, which always confuses me but here is what I have right now:
select customer.id from customer c
INNER JOIN device d on u.id = d.user_id
INNER JOIN
(select customer_id from purchase_history
where item_id = {{itemID}}
) c
on c.customer = customer_id.purchase_history
where created_date between {{starts}} and {{ends}}
group by customer_id
Thank you!!
The main goal is to provide a date range, and an item ID that will then pull the list of new customers (first code) that purchased that certain item (second code) over a certain date range. The three tables (customer, device, and purchase history) all have the customer ID column in them.