๐Ÿ’ SQL/Sum, Max, Min

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] SQL ๊ณ ๋“์  kit: Sum, Max, Min (ANIMAL_INS ํ…Œ์ด๋ธ”)

์„ ๋‹ฌ 2022. 9. 21. 19:36
๋ฐ˜์‘ํ˜•

     

    https://school.programmers.co.kr/learn/courses/30/parts/17042

     

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

    ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

    programmers.co.kr

     

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

     

    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

     

     

     

    ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ

    ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์€ ์–ธ์ œ ๋“ค์–ด์™”๋Š”์ง€ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

    -- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
    SELECT DATETIME
    from ANIMAL_INS
    where DATETIME >= all(select DATETIME from ANIMAL_INS)

     

    ์ตœ์†Ÿ๊ฐ’ ๊ตฌํ•˜๊ธฐ

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

    -- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
    SELECT DATETIME
    from ANIMAL_INS
    where DATETIME <= all(select DATETIME from ANIMAL_INS)

     

    ๋™๋ฌผ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

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

    -- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
    SELECT count(ANIMAL_ID)
    from ANIMAL_INS

     

    ์ค‘๋ณต ์ œ๊ฑฐํ•˜๊ธฐ

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

    -- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
    SELECT count(distinct NAME)
    from ANIMAL_INS
    ๋ฐ˜์‘ํ˜•