ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • SQL) ์ง‘๊ณ„ํ•จ์ˆ˜(Aggregate Functions)
    SQL 2023. 3. 21. 04:48
    COUNT
    SELECT COUNT(column)
    FROM tablename
    WHERE condition;โ€‹
    SUM
    SELECT SUM(column)
    FROM tablename
    WHERE condition;
    AVG
    SELECT AVG(column)
    FROM tablename
    WHERE condition;
    MIN, MAX
    SELECT MIN|MAX(column)
    FROM tablename
    WHERE condition;
    GROUP BY
    SELECT column1, column2, ...
    FROM table
    WHERE condition
    GROUP BY column1, column2, ...
    ORDER BY column1, column2, ...;

    ๐Ÿ”” crime_status์—์„œ ๊ฒฝ์ฐฐ์„œ๋ณ„๋กœ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ๊ฒฝ์ฐฐ์„œ ์ด๋ฆ„์„ ์กฐํšŒ

    SELECT police_station
    FROM crime_status
    GROUP BY police_station
    ORDER BY police_station;

    ๐Ÿ’ฅ DISTINCT๋ฅผ ํ†ตํ•ด์„œ ๊ฒฝ์ฐฐ์„œ ์ข…๋ฅ˜ ๊ฒ€์ƒ‰๋„ ๊ฐ€๋Šฅํ•˜๋‚˜, ORDER BY ํ•  ์ˆ˜ ์—†์Œ

    SELECT DISTINCT police_station FROM crime_status;

     

    ๐Ÿ”” ๊ฒฝ์ฐฐ์„œ๋ณ„ ํ‰๊ท  ๋ฒ”์ฃ„ ๊ฒ€๊ฑฐ ๊ฑด์ˆ˜ ๊ฒ€์ƒ‰

    SELECT police_station, avg(case_number) ํ‰๊ท ๊ฒ€๊ฑฐ๊ฑด์ˆ˜
    FROM crime_status
    WHERE status_type LIKE '๊ฒ€๊ฑฐ'
    GROUP BY police_station
    ORDER BY ํ‰๊ท ๊ฒ€๊ฑฐ๊ฑด์ˆ˜ DESC

    ๐Ÿ”” ๊ฒฝ์ฐฐ์„œ๋ณ„ ํ‰๊ท  ๋ฒ”์ฃ„ ๊ฒ€๊ฑฐ ๊ฑด์ˆ˜, ํ‰๊ท  ๋ฒ”์ฃ„ ๊ฒ€๊ฑฐ ๊ฑด์ˆ˜ ๊ฒ€์ƒ‰

    SELECT police_station, status_type, avg(case_number)
    FROM crime_status
    GROUP BY police_station, status_type;
    HAVING : ์กฐ๊ฑด์— ์ง‘๊ณ„ํ•จ์ˆ˜๊ฐ€ ํฌํ•จ๋˜๋Š” ๊ฒฝ์šฐ WHERE ๋Œ€์‹  HAVING ์‚ฌ์šฉ
    SELECT column1, column2, ...
    FROM table
    WHERE condition
    GROUP BY column1, column2, ...
    HAVING condition (Aggregate Functions)
    ORDER BY column1, column2, ...

    ๐Ÿ”” ๊ฒฝ์ฐฐ์„œ๋ณ„ ๋ฐœ์ƒํ•œ ๋ฒ”์ฃ„ ๊ฑด์ˆ˜์˜ ํ•ฉ์ด 4000๊ฑด๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ ๊ฒ€์ƒ‰

    SELECT police_station, sum(case_number) count
    FROM crime_status
    WHERE status_type LIKE '๋ฐœ์ƒ'
    GROUP BY police_station
    HAVING count > 4000;

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

    SQL) Subquery  (0) 2023.03.22
    SQL) Scalar Functions  (0) 2023.03.21
    SQL) PRIMARY KEY, FOREIGN KEY  (1) 2023.03.21
    SQL) Python with MySQL/CSV  (0) 2023.03.20
    SQL) RDS ์ ‘์†, SQL FILE ์‹คํ–‰, ๋ฐฑ์—…  (0) 2023.03.17

    ๋Œ“๊ธ€

binlog