Skip to content

Commit b2ac965

Browse files
committed
Upload 181
1 parent 62b947e commit b2ac965

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- markdownlint-disable MD041 -->
22
[![eevveerryyddaayy release](https://img.shields.io/badge/eevveerryyddaayy-v1.3.0-blue.svg)](https://github.com/ggeerraarrdd/eevveerryyddaayy/)
3-
[![Solved](https://img.shields.io/badge/solved-180-green.svg)](#index)
3+
[![Solved](https://img.shields.io/badge/solved-181-green.svg)](#index)
44
<!-- markdownlint-enable MD041 -->
55

66
# SQL Everyday
@@ -214,6 +214,7 @@ Because this project necessitated a framework to enable consistent daily practic
214214
| 178 | [Shortest Distance in a Line](https://leetcode.com/problems/shortest-distance-in-a-line/description/) | [Solution](solutions/178_shortest_distance_in_a_line.md) | LeetCode | Easy | `SELF-JOIN` vs `CROSS JOIN` |
215215
| 179 | [Friendly Movies Streamed Last Month](https://leetcode.com/problems/friendly-movies-streamed-last-month/description/) | [Solution](solutions/179_friendly_movies_streamed_last_month.md) | LeetCode | Easy | |
216216
| 180 | [Books with NULL Ratings](https://leetcode.com/problems/books-with-null-ratings/description/) | [Solution](solutions/180_books_with_null_ratings.md) | LeetCode | Easy | |
217+
| 181 | [Customer Order Frequency](https://leetcode.com/problems/customer-order-frequency/description/) | [Solution](solutions/181_customer_order_frequency.md) | LeetCode | Easy | `USING`, `HAVING` |
217218
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
218219
<!--- cSpell:enable --->
219220

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# SQL Everyday \#181
2+
3+
## Customer Order Frequency
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Easy
7+
8+
## Problem
9+
10+
Write a solution to report the `customer_id` and `customer_name` of customers who have spent at least `$100` in each month of **June and July 2020**.
11+
12+
Return the result table in **any order**. [[Full Description](https://leetcode.com/problems/customer-order-frequency/description/)]
13+
14+
## Submitted Solution
15+
16+
```sql
17+
-- Submitted Solution
18+
WITH cte1 AS (
19+
SELECT
20+
c.customer_id
21+
,c.name
22+
,DATE_TRUNC('month', o.order_date) AS month
23+
,SUM(o.quantity * p.price) AS total
24+
FROM Orders AS o
25+
JOIN Product AS p ON o.product_id = p.product_id
26+
JOIN Customers AS c ON o.customer_id = c.customer_id
27+
GROUP BY c.customer_id, c.name, month
28+
),
29+
cte2 AS (
30+
SELECT
31+
customer_id
32+
,name
33+
,SUM(CASE WHEN (month = '2020-06-01' or month = '2020-07-01') AND total >= 100 THEN 1
34+
ELSE 0 END) AS validation
35+
FROM cte1
36+
GROUP BY customer_id, name
37+
)
38+
SELECT
39+
customer_id
40+
,name
41+
FROM cte2
42+
WHERE validation = 2
43+
;
44+
```
45+
46+
## Site Solution
47+
48+
```sql
49+
-- LeetCode Solution
50+
SELECT
51+
customer_id,
52+
name
53+
FROM
54+
Customers
55+
JOIN Orders USING(customer_id)
56+
JOIN Product USING(product_id)
57+
WHERE
58+
YEAR(order_date)= 2020
59+
GROUP BY
60+
customer_id
61+
HAVING
62+
SUM(
63+
IF(MONTH(order_date) = 6, quantity, 0) * price
64+
) >= 100 AND
65+
SUM(
66+
IF(MONTH(order_date) = 7, quantity, 0) * price
67+
) >= 100;
68+
```
69+
70+
## Notes
71+
72+
TBD
73+
74+
## NB
75+
76+
`USING`, `HAVING`
77+
78+
Go to [Index](../?tab=readme-ov-file#index)\
79+
Go to [Overview](../?tab=readme-ov-file)

0 commit comments

Comments
 (0)