๐ ์ฐ์ ์ ์๊ฑฐํธ๊ฐ ๋ด๊ธด ์ฅ๋ฐ๊ตฌ๋
๋ฐ์ดํฐ ๋ถ์ ํ์์๋ ์ฐ์ (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)
๋ณดํธ์์์๋ ๋ช ์์ ์ ์์ด ๊ฐ์ฅ ํ๋ฐํ๊ฒ ์ผ์ด๋๋์ง ์์๋ณด๋ ค ํฉ๋๋ค.
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
๐ ๋ณดํธ์์์ ์ค์ฑํํ ๋๋ฌผ
๋ณดํธ์์์ ์ค์ฑํ ์์ ์ ๊ฑฐ์น ๋๋ฌผ ์ ๋ณด๋ฅผ ์์๋ณด๋ ค ํฉ๋๋ค.
๋ณดํธ์์ ๋ค์ด์ฌ ๋น์์๋ ์ค์ฑํ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 |
๋๊ธ