suvera-dev ๐Ÿฅฆ

SQL) ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค SQL ๊ณ ๋“์  kit - GROUP BY ๋ณธ๋ฌธ

Algorithm/CodingTest - SQL

SQL) ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค SQL ๊ณ ๋“์  kit - GROUP BY

suvera 2022. 4. 15. 03:40

 

1. ๊ณ ์–‘์ด์™€ ๊ฐœ๋Š” ๋ช‡๋งˆ๋ฆฌ ์žˆ์„๊นŒ

๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ค‘ ๊ณ ์–‘์ด์™€ ๊ฐœ๊ฐ€ ๊ฐ๊ฐ ๋ช‡ ๋งˆ๋ฆฌ์ธ์ง€ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ด๋•Œ ๊ณ ์–‘์ด๋ฅผ ๊ฐœ๋ณด๋‹ค ๋จผ์ € ์กฐํšŒํ•ด์ฃผ์„ธ์š”.

SELECT ANIMAL_TYPE , COUNT(ANIMAL_TYPE)
FROM ANIMAL_INS
GROUP BY ANIMAL_TYPE
ORDER BY ANIMAL_TYPE

 

 

 

2. ๋™๋ช… ๋™๋ฌผ ์ˆ˜ ์ฐพ๊ธฐ 

๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ด๋ฆ„ ์ค‘ ๋‘ ๋ฒˆ ์ด์ƒ ์“ฐ์ธ ์ด๋ฆ„๊ณผ ํ•ด๋‹น ์ด๋ฆ„์ด ์“ฐ์ธ ํšŸ์ˆ˜๋ฅผ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ์ด๋ฆ„์ด ์—†๋Š” ๋™๋ฌผ์€ ์ง‘๊ณ„์—์„œ ์ œ์™ธํ•˜๋ฉฐ, ๊ฒฐ๊ณผ๋Š” ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์กฐํšŒํ•ด์ฃผ์„ธ์š”.

- ์ด๋ฆ„์ด ์—†๋Š” ๋™๋ฌผ ์ œ์™ธ, ์ด๋ฆ„๋ณ„๋กœ ๊ทธ๋ฃน ๋ฌถ์–ด์ฃผ๊ณ , HAVING ์ ˆ์—์„œ 2๋ฒˆ ์ด์ƒ ์“ฐ์ธ๊ฑฐ ๊ฑธ๋Ÿฌ์ฃผ๊ธฐ.

SELECT NAME, COUNT(NAME)
FROM ANIMAL_INS
WHERE NAME IS NOT NULL
GROUP BY NAME
HAVING COUNT(NAME) >= 2
ORDER BY NAME

 


 

 

 

3. ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ(1)

๋ณดํ˜ธ์†Œ์—์„œ๋Š” ๋ช‡ ์‹œ์— ์ž…์–‘์ด ๊ฐ€์žฅ ํ™œ๋ฐœํ•˜๊ฒŒ ์ผ์–ด๋‚˜๋Š”์ง€ ์•Œ์•„๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค. 09:00๋ถ€ํ„ฐ 19:59๊นŒ์ง€, ๊ฐ ์‹œ๊ฐ„๋Œ€๋ณ„๋กœ ์ž…์–‘์ด ๋ช‡ ๊ฑด์ด๋‚˜ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ์‹œ๊ฐ„๋Œ€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

SELECT HOUR(DATETIME) HOUR, COUNT(DATETIME) COUNT
FROM ANIMAL_OUTS
WHERE HOUR(DATETIME) >= 9 AND HOUR(DATETIME) <= 19
GROUP BY HOUR(DATETIME)
ORDER BY HOUR

- HOUR(DATETIME) ์œผ๋กœ ์‹œ๊ฐ„๋งŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ. ๊ทธ๋ฆฌ๊ณ  ์‹œ๊ฐ„๋ณ„๋กœ ๊ทธ๋ฃนํ™”ํ•˜๊ณ 

WHERE ์ ˆ์—์„œ 9์‹œ ์ด์ƒ 19์‹œ์ดํ•˜๋กœ ์กฐ๊ฑด ๊ฑธ๊ธฐ. + ์ •๋ ฌ

 

 

4. ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ(2)

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

๋ ˆ๋ฒจ 4 ๋ฌธ์ œ

0๋ถ€ํ„ฐ 23์‹œ๊นŒ์ง€ ์ถœ๋ ฅํ•ด์•ผ๋˜๋Š”๋ฐ ํ•ด๋‹น ๋ฐ์ดํ„ฐ์—์„œ๋Š” 7์‹œ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•œ๋‹ค.

0-6์‹œ๊นŒ์ง€์˜ ๊ฒฐ๊ณผ๋Š” ์ถœ๋ ฅ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋”ฐ๋กœ ์ƒ์„ฑํ•ด์ค˜์•ผ ํ•œ๋‹ค !

 

์ด๋•Œ SET ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค 

SET์€ ์–ด๋–ค ๋ณ€์ˆ˜์— ํŠน์ • ๊ฐ’์„ ํ• ๋‹นํ•  ๋•Œ ์“ฐ๋Š” ๋ช…๋ น์–ด !

 

SET @HOUR = -1;
SELECT (@HOUR := @HOUR +1) AS HOUR
FROM ANIMAL_OUTS
WHERE @HOUR < 23;

๋‹ค์Œ ์ฝ”๋“œ๋Š” @HOUR๋ณ€์ˆ˜์— -1์„ ํ• ๋‹นํ•œ ํ›„

22๊นŒ์ง€ +1์”ฉ ๋”ํ•ด์ค€๋‹ค๋Š” ๋œป์ด๋‹ค! 

@๋Š” ๋ณ€์ˆ˜๋ช… ์•ž์— ๋„ฃ์–ด์ค€๋‹ค.

 

 WHERE์ ˆ์ธ 23 ๋ฏธ๋งŒ์ผ๋•Œ๊นŒ์ง€ ๋Œ€์ž…๋˜์–ด 

HOUR์ด 22 ๊ฐ€ ๋ ๋•Œ ๋ฉˆ์ถ”๋ฉฐ 22๋Š” 22+1 ์ธ 23์ด ๋œ๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ฃผ์˜๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. 

SET ์‚ฌ์šฉ์‹œ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ '='๋ฅผ ์‚ฌ์šฉํ•˜๊ณ 

๊ทธ ์™ธ์—๋Š” := ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค

 

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด 0๋ถ€ํ„ฐ 23๊นŒ์ง€ ์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๊ฐ€ ์กฐํšŒ๋œ๋‹ค !

 

SET @HOUR := -1; # ๋ณ€์ˆ˜์„ ์–ธ

SELECT (@HOUR := @HOUR +1) AS HOUR,
(SELECT COUNT(*) 
FROM ANIMAL_OUTS 
WHERE HOUR(DATETIME) = @HOUR) AS COUNT 
FROM ANIMAL_OUTS
WHERE @HOUR < 23

์„œ๋ธŒ ์ฟผ๋ฆฌ๋กœ ์‹œ๊ฐ„์— ๋”ฐ๋ฅธ COUNT๋ฅผ ์ง‘๊ณ„ํ•ด์ฃผ๋Š”๋ฐ

 

WHERE HOUR(DATETIME) = @HOUR ์—ฌ๊ธฐ์„œ

 

ANIMAL_OUTS ํ…Œ์ด๋ธ”์— ์žˆ๋Š” DATETIME ๋ณ€์ˆ˜์™€

@HOUR ๋ณ€์ˆ˜๊ฐ€ ๋™์ผํ•œ ์ˆœ๊ฐ„์„ ์กฐ๊ฑด์œผ๋กœ ๊ฑธ์–ด์„œ ์นด์šดํŠธ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

Comments