Last active
December 26, 2017 13:19
-
-
Save rummykhan/9109ed92ad74cfa146d048297735aa09 to your computer and use it in GitHub Desktop.
Get all the customers who placed their first order this month
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Schema | |
------ | |
customer | |
id | |
username | |
created_at | |
order | |
id, | |
customer_id | |
created_at | |
Scenario | |
-------- | |
Get all customers who placed their first order this month irrespective of their registration date. | |
Query - A | |
--------- | |
SELECT | |
COUNT(CASE WHEN DATE_FORMAT(order.created_at, '%y-%m') < '17-12' THEN order.id END) previous_order, | |
COUNT(CASE WHEN DATE_FORMAT(order.created_at, '%y-%m') > '17-11' THEN order.id END) orders, | |
customer.id | |
FROM | |
order | |
INNER JOIN customer ON order.customer_id=customer.id | |
GROUP BY | |
customer.id | |
HAVING | |
previous_order = 0 | |
AND orders > 0 | |
Query - B | |
--------- | |
SELECT | |
customer.id, | |
COUNT(order.id) requests | |
FROM | |
order | |
INNER JOIN | |
customer ON order.customer_id = customer.id | |
WHERE | |
customer.id NOT IN (select customer_id from order where DATE_FORMAT(order.created_at, '%y-%m') < '17-12') | |
GROUP BY | |
customer.id | |
Benchmarking (ms) | |
----------- | |
A B | |
----------- | |
109 | 141 | |
125 | 140 | |
125 | 141 | |
109 | 125 | |
109 | 125 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment