Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[LeetCode] 1045. Customers Who Bought All Products

์ธ” 2023. 5. 18. 12:23

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ Problems, https://leetcode.com/problemset/all/์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.


โœ”๋ฌธ์ œ

https://leetcode.com/problems/customers-who-bought-all-products/

 

Customers Who Bought All Products - LeetCode

Can you solve this real interview question? Customers Who Bought All Products - Table: Customer +-------------+---------+ | Column Name | Type | +-------------+---------+ | customer_id | int | | product_key | int | +-------------+---------+ There is no pri

leetcode.com

Table: Customer

Table: Product

Write an SQL query to report the customer ids from the Customer table that bought all the products in the Product table.

Return the result table in any order.

The query result format is in the following example.


โœ” ํ’€์ด

  ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ  

Product ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๋ชจ๋“  product๋ฅผ ๊ตฌ๋งคํ•œ ๊ณ ๊ฐ์˜ id๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

 

์ด๋ฒˆ ๋ฌธ์ œ์—์„œ๋Š” MySQL์—์„œ ์ œ๊ณตํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ํ•ฉ์น˜๋Š” ๋ฌธ๋ฒ• GROUP_CONCAT()์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

โœ” ๋ฌธ๋ฒ• :
GROUP_CONCAT(์ปฌ๋Ÿผ๋ช… ORDER BY ์ปฌ๋Ÿผ๋ช… SEPARATOR ๊ตฌ๋ถ„์ž)

· SEPARATOR ๊ธฐ๋ณธ๊ฐ’์€ ','
· CONCAT ํ•  ์ปฌ๋Ÿผ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์ •๋ ฌ ์กฐ๊ฑด์„ ๋ถ€์—ฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ, ORDER BY ์ปฌ๋Ÿผ๋ช…์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

โ‘  Customerํ…Œ์ด๋ธ”์—์„œ id๋ณ„๋กœ ์‚ฐ product_key๋ฅผ group_concat์„ ํ™œ์šฉํ•ด์„œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์ฒ˜๋Ÿผ pd_keys ์ปฌ๋Ÿผ์— ๋‹ด๊ณ 

โ‘ก id๋ณ„๋กœ ๋‹ด์€ pd_keys๊ฐ€ Product ํ…Œ์ด๋ธ”์˜ product_key์ปฌ๋Ÿผ ๊ฐ’์„ ํ•ฉ์นœ pd_keys ์ปฌ๋Ÿผ๊ณผ ๋™์ผํ•œ ์ง€¹ ๋น„๊ตํ•ด์„œ ๋™์ผํ•œ ๊ณ ๊ฐ์˜ ID๋งŒ ์ถœ๋ ฅํ•œ๋‹ค.

SELECT customer_id
 FROM (
    SELECT customer_id, GROUP_CONCAT(DISTINCT product_key ORDER BY product_key SEPARATOR ',') AS pd_keys
    FROM Customer
    GROUP BY 1) T1
 WHERE pd_keys = (SELECT GROUP_CONCAT(product_key ORDER BY product_key SEPARATOR ',') AS pd_keys
                   FROM Product)
                   ;

¹ product_key๊ฐ€ ๋ชจ๋‘ ์žˆ๋‹ค๋Š” ๊ฒƒ = ๋ชจ๋“  ์ƒํ’ˆ์„ ๊ตฌ๋งคํ–ˆ๋‹ค. ๋Š” ์˜๋ฏธ

 

 

<์ฐธ๊ณ ์ž๋ฃŒ>

์ฐธ๊ณ  ๋งํฌ์—์„œ Oracle์—์„œ ๋™์ผํ•œ ์—ญํ• ์„ ํ•˜๋Š” listagg ์— ๋Œ€ํ•ด์„œ๋„ ๊ณต๋ถ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

[SQL] ๊ทธ๋ฃน๋ณ„ ๋ฌธ์ž์—ด ๋ฌถ๊ธฐ (group_concat, listag)

MySQL ๊ทธ๋ฃน๋ณ„ ๋ฌธ์ž์—ด ๋ฌถ๊ธฐ ๊ทธ๋ฃน๋ณ„๋กœ ์ปฌ๋Ÿผ ๋ฐ์ดํ„ฐ(๋ฌธ์ž์—ด)๋ฅผ ๋ฌถ์–ด์„œ ๋ณด๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ MySQL์ด๋‚˜ Oracle์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๋น„๊ตํ•ด์„œ ์•Œ๋ ค๋“œ๋ฆฌ๋„๋ก

codingspooning.tistory.com


โœ ๊ฐœ์ธํšŒ๊ณ 

๋ฌธ์ œ ํ’€์ด์— ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ๋‘๋ฒˆ์ด๋‚˜ ์‚ฌ์šฉํ•ด์„œ ์ฟผ๋ฆฌ ์‹คํ–‰์— ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ๋“ฏ ํ•˜๋‹ค.

 

'Growth ๐ŸŒณ > Practice ๐Ÿ’ป' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] 1158. Market Analysis I  (0) 2023.06.08
[LeetCode] 1070. Product Sales Analysis III  (0) 2023.05.22
[LeetCode] 608. Tree Node  (0) 2023.05.16
[LeetCode] 602. Friend Requests II: Who Has the Most Friends  (0) 2023.05.12
[LeetCode] 601. Human Traffic of Stadium  (0) 2023.05.11