๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SQL

[SQL] Programmers : Level 4

by ํฌ๊ตฌ๋ฆฌ 2021. 3. 26.

๐Ÿ“Œ ์šฐ์œ ์™€ ์š”๊ฑฐํŠธ๊ฐ€ ๋‹ด๊ธด ์žฅ๋ฐ”๊ตฌ๋‹ˆ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์šฐ์œ ์™€ ์š”๊ฑฐํŠธ๊ฐ€ ๋‹ด๊ธด ์žฅ๋ฐ”๊ตฌ๋‹ˆ

CART_PRODUCTS ํ…Œ์ด๋ธ”์€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ์ƒํ’ˆ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. CART_PRODUCTS ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ID, CART_ID, NAME, PRICE๋Š” ๊ฐ๊ฐ ํ…Œ์ด๋ธ”์˜ ์•„์ด๋””, ์žฅ๋ฐ”๊ตฌ๋‹ˆ์˜ ์•„์ด๋””, ์ƒํ’ˆ ์ข…๋ฅ˜, ๊ฐ€

programmers.co.kr

๋ฐ์ดํ„ฐ ๋ถ„์„ ํŒ€์—์„œ๋Š” ์šฐ์œ (Milk)์™€ ์š”๊ฑฐํŠธ(Yogurt)๋ฅผ ๋™์‹œ์— ๊ตฌ์ž…ํ•œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

์šฐ์œ ์™€ ์š”๊ฑฐํŠธ๋ฅผ ๋™์‹œ์— ๊ตฌ์ž…ํ•œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์˜ ์•„์ด๋””๋ฅผ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ์žฅ๋ฐ”๊ตฌ๋‹ˆ์˜ ์•„์ด๋”” ์ˆœ์œผ๋กœ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

SELECT CART_ID
FROM CART_PRODUCTS
WHERE NAME IN ('Milk', 'Yogurt')
GROUP BY CART_ID
HAVING COUNT(DISTINCT NAME) >= 2
ORDER BY CART_ID

 

 

๐Ÿ“Œ ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ(2)

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ(2)

ANIMAL_OUTS ํ…Œ์ด๋ธ”์€ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์—์„œ ์ž…์–‘ ๋ณด๋‚ธ ๋™๋ฌผ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ANIMAL_OUTS ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ANIMAL_ID, ANIMAL_TYPE, DATETIME, NAME, SEX_UPON_OUTCOME๋Š” ๊ฐ๊ฐ ๋™๋ฌผ์˜ ์•„์ด๋””, ์ƒ๋ฌผ

programmers.co.kr

๋ณดํ˜ธ์†Œ์—์„œ๋Š” ๋ช‡ ์‹œ์— ์ž…์–‘์ด ๊ฐ€์žฅ ํ™œ๋ฐœํ•˜๊ฒŒ ์ผ์–ด๋‚˜๋Š”์ง€ ์•Œ์•„๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

0์‹œ๋ถ€ํ„ฐ 23์‹œ๊นŒ์ง€, ๊ฐ ์‹œ๊ฐ„๋Œ€๋ณ„๋กœ ์ž…์–‘์ด ๋ช‡ ๊ฑด์ด๋‚˜ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ์‹œ๊ฐ„๋Œ€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

WITH RECURSIVE TIME AS (
    SELECT 0 AS H
    UNION ALL
    SELECT H+1 
    FROM TIME
    WHERE H < 23
)

SELECT H, COUNT(HOUR(DATETIME)) AS 'COUNT'
FROM TIME
LEFT OUTER JOIN ANIMAL_OUTS
ON H = HOUR(DATETIME)
GROUP BY H

 

 

๐Ÿ“Œ ๋ณดํ˜ธ์†Œ์—์„œ ์ค‘์„ฑํ™”ํ•œ ๋™๋ฌผ

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ณดํ˜ธ์†Œ์—์„œ ์ค‘์„ฑํ™”ํ•œ ๋™๋ฌผ

ANIMAL_INS ํ…Œ์ด๋ธ”์€ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ANIMAL_INS ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, SEX_UPON_INTAKE๋Š” ๊ฐ๊ฐ ๋™๋ฌผ์˜ ์•„์ด๋””

programmers.co.kr

๋ณดํ˜ธ์†Œ์—์„œ ์ค‘์„ฑํ™” ์ˆ˜์ˆ ์„ ๊ฑฐ์นœ ๋™๋ฌผ ์ •๋ณด๋ฅผ ์•Œ์•„๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜ฌ ๋‹น์‹œ์—๋Š” ์ค‘์„ฑํ™”1๋˜์ง€ ์•Š์•˜์ง€๋งŒ, ๋ณดํ˜ธ์†Œ๋ฅผ ๋‚˜๊ฐˆ ๋‹น์‹œ์—๋Š” ์ค‘์„ฑํ™”๋œ ๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ƒ๋ฌผ ์ข…, ์ด๋ฆ„์„ ์กฐํšŒํ•˜๋Š” ์•„์ด๋”” ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ค‘์„ฑํ™”ํ–ˆ์œผ๋ฉด ์„ฑ๋ณ„์ด ๋ฐ”๋€Œ์—ˆ์„ ๊ฒƒ์ด๋ฏ€๋กœ !=๋กœ ํ–ˆ๋‹ค.

 

SELECT OUTS.ANIMAL_ID, OUTS.ANIMAL_TYPE, OUTS.NAME
FROM ANIMAL_OUTS AS OUTS
INNER JOIN ANIMAL_INS AS INS
ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
WHERE INS.SEX_UPON_INTAKE != OUTS.SEX_UPON_OUTCOME
ORDER BY INS.ANIMAL_ID

 

LIKE, AND ์‚ฌ์šฉ
SELECT INS.ANIMAL_ID, INS.ANIMAL_TYPE, INS.NAME
FROM ANIMAL_INS AS INS
LEFT JOIN ANIMAL_OUTS AS OUTS
USING (ANIMAL_ID)
WHERE (INS.SEX_UPON_INTAKE NOT LIKE '%Spayed%' AND INS.SEX_UPON_INTAKE NOT LIKE '%Neutered%') AND (OUTS.SEX_UPON_OUTCOME LIKE '%Spayed%' OR OUTS.SEX_UPON_OUTCOME LIKE '%Neutered%')
ORDER BY INS.ANIMAL_ID

 

'SQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[SQL] Programmers : Level 3  (0) 2021.03.25
[SQL] Programmers : Level 2(2)  (0) 2021.03.10
[SQL] Programmers : Level 2(1)  (0) 2021.03.10
[SQL] Programmers : Level 1(2)  (0) 2021.03.09
[SQL] Programmers : Level 1(1)  (0) 2021.03.08

๋Œ“๊ธ€