Skip to content
Advertisement

SQL – min(date) with conditionals

We ran a promotion where users can receive their first subscription order free. Price = $0.00 when a user uses the promo. I am interested in the data from Example A.

Example A – User 50 started with the promo and continued for two months

Example B – User 100 was already an active subscriber who cancelled his account and reactivated with the promo, I do not wish to count him

–Here is my query–

enter image description here

This returns all users who have multiple orders

WHERE at least one of their orders has a price = 0.00

-This dataset returns example A and example B

–My question–

Most of this data is correct (Example A) but a handful of them I want to omit because they are skewing my data (Example B). I want to remove Example B users.

I want to remove people who’s first order was not the promo.

How can I request that their FIRST order had a price = 0.00? I was thinking something with min(created_at)?

Advertisement

Answer

You can get the time of the first order using:

Next, you can get the price of the first order using:

Then you can get all records, using join, in, or exists;

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement