Skip to content

Commit b704025

Browse files
committed
Upload 173
1 parent 3b5dda3 commit b704025

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-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-172-green.svg)](#index)
3+
[![Solved](https://img.shields.io/badge/solved-173-green.svg)](#index)
44
<!-- markdownlint-enable MD041 -->
55

66
# SQL Everyday
@@ -206,6 +206,7 @@ Because this project necessitated a framework to enable consistent daily practic
206206
| 170 | [Game Play Analysis II](https://leetcode.com/problems/game-play-analysis-ii/description/) | [Solution](solutions/170_game_play_analysis_ii.md) | LeetCode | Easy | |
207207
| 171 | [Total Traveled Distance](https://leetcode.com/problems/total-traveled-distance/description/) | [Solution](solutions/171_total_traveled_distance.md) | LeetCode | Easy | |
208208
| 172 | [Create a Session Bar Chart](https://leetcode.com/problems/create-a-session-bar-chart/description/) | [Solution](solutions/172_create_a_session_bar_chart.md) | LeetCode | Easy | Histogram bins w/ `UNION`, `CASE` |
209+
| 173 | [Customers Who Bought Products A and B but Not C](https://leetcode.com/problems/customers-who-bought-products-a-and-b-but-not-c/description/) | [Solution](solutions/173_customers_who_bought_products_a_and_b_but_not_c.md) | LeetCode | Medium | `STRING_AGG` |
209210
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
210211
<!--- cSpell:enable --->
211212

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# SQL Everyday \#173
2+
3+
## Customers Who Bought Products A and B but Not C
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Medium
7+
8+
## Problem
9+
10+
Write a solution to report the customer_id and customer_name of customers who bought products *"A"*, *"B"* but did not buy the product *"C"* since we want to recommend them to purchase this product.
11+
12+
Return the result table *ordered* by `customer_id`. [[Full Description](https://leetcode.com/problems/customers-who-bought-products-a-and-b-but-not-c/description/)]
13+
14+
## Submitted Solution
15+
16+
```sql
17+
-- Submitted Solution
18+
WITH cte AS (
19+
SELECT
20+
customer_id
21+
,STRING_AGG(product_name, ',') AS products
22+
FROM Orders
23+
GROUP BY customer_id
24+
)
25+
SELECT
26+
c.customer_id
27+
,c.customer_name
28+
FROM Customers AS c
29+
LEFT JOIN cte ON c.customer_id = cte.customer_id
30+
WHERE (cte.products LIKE '%A%'
31+
AND cte.products LIKE '%B%'
32+
AND cte.products NOT LIKE '%C%')
33+
ORDER BY c.customer_id
34+
;
35+
```
36+
37+
## Site Solution
38+
39+
```sql
40+
-- LeetCode Solution
41+
SELECT c.customer_id, customer_name
42+
FROM customers c
43+
LEFT JOIN orders o ON c.customer_id = o.customer_id
44+
GROUP BY c.customer_id
45+
HAVING SUM(product_name='A') > 0
46+
AND SUM(product_name='B') > 0
47+
AND SUM(product_name='C') = 0
48+
ORDER BY c.customer_id;
49+
```
50+
51+
## Notes
52+
53+
TBD
54+
55+
## NB
56+
57+
`STRING_AGG`
58+
59+
Go to [Index](../?tab=readme-ov-file#index)\
60+
Go to [Overview](../?tab=readme-ov-file)

0 commit comments

Comments
 (0)