๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด(SQL)

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(2)

by carrot0911 2024. 11. 22.

๋ฌธ์ œ ์„ค๋ช…

https://school.programmers.co.kr/learn/courses/30/lessons/59411

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

ANIMAL_INS ํ…Œ์ด๋ธ”์€ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ANIMAL_INS ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, 
ANIMAL_IDANIMAL_TYPEDATETIMEINTAKE_CONDITIONNAMESEX_UPON_INTAKE๋Š” ๊ฐ๊ฐ ๋™๋ฌผ์˜ ์•„์ด๋””, ์ƒ๋ฌผ ์ข…, ๋ณดํ˜ธ ์‹œ์ž‘์ผ, ๋ณดํ˜ธ ์‹œ์ž‘ ์‹œ ์ƒํƒœ, ์ด๋ฆ„, ์„ฑ๋ณ„ ๋ฐ ์ค‘์„ฑํ™” ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

NAME TYPE NULLABLE
ANIMAL_ID VARCHAR(N) FALSE
ANIMAL_TYPE VARCHAR(N) FALSE
DATETIME DATETIME FALSE
INTAKE_CONDITION VARCHAR(N) FALSE
NAME VARCHAR(N) TRUE
SEX_UPON_INTAKE VARCHAR(N) FALSE

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

NAME TYPE NULLABLE
ANIMAL_ID VARCHAR(N) FALSE
ANIMAL_TYPE VARCHAR(N) FALSE
DATETIME DATETIME FALSE
NAME VARCHAR(N) TRUE
SEX_UPON_OUTCOME VARCHAR(N) FALSE

๋ฌธ์ œ

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

์˜ˆ์‹œ

์˜ˆ๋ฅผ ๋“ค์–ด, ANIMAL_INS ํ…Œ์ด๋ธ”๊ณผ ANIMAL_OUTS ํ…Œ์ด๋ธ”์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๋ฉด

ANIMAL_INS

ANIMAL
_ID
ANIMAL
_TYPE
DATETIME INTAKE
_CONDITION
NAME SEX_UPON
_INTAKE
A354597 Cat 2014-05-02
12:16:00
Normal Ariel Spayed Female
A362707 Dog 2016-01-27
12:27:00
Sick Girly Girl Spayed Female
A370507 Cat 2014-10-27
14:43:00
Normal Emily Spayed Female
A414513 Dog 2016-06-07
09:17:00
Normal Rocky Neutered Male

ANIMAL_OUTS 

ANIMAL_ID ANIMAL_TYPE DATETIME NAME SEX_UPON
_OUTCOME
A354597 Cat 2014-06-03 12:30:00 Ariel Spayed Female
A362707 Dog 2017-01-10 10:44:00 Girly Girl Spayed Female
A370507 Cat 2015-08-15 09:24:00 Emily Spayed Female

SQL๋ฌธ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ANIMAL_ID NAME
A362707 Girly Girl
A370507 Emily

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

WITH DATETIME AS (
    SELECT AI.ANIMAL_ID, AI.NAME, DATEDIFF(AO.DATETIME, AI.DATETIME) AS DATETIME
    FROM ANIMAL_INS AI LEFT JOIN ANIMAL_OUTS AO ON AI.ANIMAL_ID=AO.ANIMAL_ID
    ORDER BY DATETIME DESC
    LIMIT 2
)

SELECT ANIMAL_ID, NAME
FROM DATETIME;

์ฝ”๋“œ ์„ค๋ช…

  • WITH DATETIME AS ( ) : ์ž„์‹œ ํ…Œ์ด๋ธ”์„ ์ •์˜ํ•œ๋‹ค. ์ดํ›„์˜ SELECT ๋ฌธ์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ž„์‹œ๋กœ ๋‹ด์•„๋‘๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค.
    • SELECT AI.ANIMMAL_ID, AI.NAME, DATEDIFF(AO.DATETIME, AI.DATETIME) AS DATETIME : ANIMAL_INS ํ…Œ์ด๋ธ”์˜ ANIMAL_ID ์ปฌ๋Ÿผ๊ณผ NAME ์ปฌ๋Ÿผ์„ ์„ ํƒํ•œ๋‹ค. DATEDIFF ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ANIMAL_OUTS ํ…Œ์ด๋ธ”์˜ DATETIME๊ณผ ANIMAL_INS ํ…Œ์ด๋ธ”์˜ DATETIME ๊ฐ„์˜ ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ DATETIME ์ปฌ๋Ÿผ์œผ๋กœ ์ •์˜ํ•œ๋‹ค.
    • FROM ANIMAL_INS AI LEFT JOIN ANIMAL_OUTS AO : ANIMAL_INS ํ…Œ์ด๋ธ”์„ AI, ANIMAL_OUTS ํ…Œ์ด๋ธ”์„ AO๋กœ ๊ฐ„๋žตํ•˜๊ฒŒ ์ •์˜ํ•˜๊ณ , LEFT JOIN์œผ๋กœ ๋‘ ํ…Œ์ด๋ธ”์„ ์—ฐ๊ฒฐํ•œ๋‹ค. LEFT JOIN์ด๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€์‘ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฉด NULL์ด ๋œ๋‹ค.
    • ON AI.ANIMAL_ID = AO.ANIMAL_ID : ANIMAL_INS ํ…Œ์ด๋ธ”์˜ ANIMAL_ID ์ปฌ๋Ÿผ๊ณผ ANIMAL_OUTS ํ…Œ์ด๋ธ”์˜ ANIMAL_ID ์ปฌ๋Ÿผ์—์„œ ์ผ์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค.
    • ORDER BY DATETIME DESC : DATETIME ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ด์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋จผ์ € ์˜ค๋„๋ก ์ •๋ ฌํ•œ๋‹ค.
    • LIMIT 2 : ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ ์ค‘ ๊ฐ€์žฅ ์ƒ์œ„์˜ ๋ฐ์ดํ„ฐ 2๊ฐœ๋งŒ ์„ ํƒํ•œ๋‹ค.
  • SELECT ANIMAL_ID, NAME : ANIMAL_ID ์ปฌ๋Ÿผ๊ณผ NAME ์ปฌ๋Ÿผ์„ ์„ ํƒํ•œ๋‹ค.
  • FROM DATETIME : ์ž„์‹œ ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ” DATETIME์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.