[2023.12.04] SQL_집계함수

하은·2023년 12월 4일
0

- Aggregate Functions(집계함수)

- 여러 칼럼 혹은 테이블 전체 칼럼으로부터 하나의 결과값을 반환하는 함수

  • Function | Description
    1) COUNT : 총 갯수를 계산해주는 함수
    2) SUM : 합계를 계산해주는 함수
    3) AVG : 평균을 계산해주는 함수
    4) MIN : 가장 작은 값을 찾아주는 함수
    5) MAX : 가장 큰 값을 찾아주는 함수
    6) FIRST : 첫번째 결과값을 리턴하는 함수
    7) LAST : 마지막 결과값을 리턴하는 함수

- COUNT

- 총 개수를 계산해주는 함수

SELECT COUNT(column)
FROM tablename
WHERE condition;

  • 예) police_station 테이블에서 데이터는 모두 몇 개?
    = SELECT COUNT(*) FROM police_station;
  • 예) crime_status 테이블에서 경찰서는 총 몇군데?
    select count(*) from police_station;
    select distinct police_station from crime_status;
  • 예) crime_type은 총 몇가지?
    select distinct crime_type from crime_status;
    select count(distinct crime_type) from crime_status;

- SUM

- 숫자 칼럼의 합계를 계산해주는 함수

SELECT SUM(column)
FROM tablename
WHERE condition;

  • 예) 범죄 총 발생건수?
    SELECT SUM(case_number) FROM crime_status WHERE status_type = '발생';
  • 예) 살인의 총 발생건수
    select sum(case_number) from crime_status where status_type='발생' and crime_type like '살인';
  • 예) 중부 경찰서에서 검거된 총 범죄 건수
    select sum(case_number) from crime_status
    where status_type = '검거' and police_station='중부';
  • 예) 종로경찰서와 남대문경찰서의 강도 발생 건수의 합
    select sum(case_number) from crime_status where (police_station = '종로' or police_station = '남대문') and status_type = '발생' and crime_type = '강도';
  • 예) 폭력범죄의 검거건수의 합
    select sum(case_number) from crime_status where crime_type = '폭력' and status_type = '검거';

- AVG

- 숫자 칼럼의 평균을 계산해주는 함수

SELECT AVG(column)
FROM tablename
WHERE condition;

  • 예) 평균 폭력 검거 건수는?
    SELECT AVG(case_number)
    FROM crime_status
    WHERE crime_type LIKE '폭력' AND status_type '검거';
    ++ 데이터
    = SELECT police_station, crime_type, status_type, case_number
    FROM crime_status
    WHERE crime_type LIKE '폭력' AND status_type = '검거';
  • 예) 중부경찰서 범죄 평균 발생 건수
    SELECT AVG(case_number)
    FROM crime_status
    WHERE police_station LIKE '중부' AND status_type '발생';
    ++ 데이터
    = SELECT police_station, crime_type, status_type, case_number
    FROM crime_status
    WHERE police_station LIKE '중부' AND status_type '발생';

- MIN

- 숫자 칼럼 중 가장 작은 값을 찾아주는 함수

SELECT MIN(column)
FROM table
WHERE condition;

  • 예) 강도 발생 건수가 가장 적은 경우는 몇 건?
    SELECT MIN(case_number)
    FROM crime_status
    WHERE crime_type LIKE '강도' AND status_type = '발생';
    ++ 데이터
    = SELECT police_station, crime_type, status_type, case_number
    FROM crime_status
    WHERE crime_type LIKE '강도' AND status_type = '발생';
  • 예) 중부경찰서에서 가장 낮은 검거 건수는?
    SELECT MIN(case_number)
    FROM crime_status
    WHERE police_station LIKE '중부' AND status_type = '검거';
    ++ 데이터
    = SELECT police_station, crime_type, status_type, case_number
    FROM crime_status
    WHERE police_station LIKE '중부' AND status_type = '검거';

- MAX

- 숫자 칼럼 중 가장 큰 값을 찾아주는 함수

SELECT MAX(column)
FROM tablename
WHERE condition;

  • 예) 살인이 가장 많이 검거된 건수는?
    SELECT MAX(case_number)
    FROM crime_status
    WHERE crime_type LIKE '살인' AND status_type LIKE '검거;
    ++ 데이터
    = SELECT police_station, crime_type, status_type, case_number
    FROM crime_status
    WHERE crime_type LIKE '살인' AND status_type LIKE '검거;
  • 예) 강남 경찰서에서 가장 많이 발생한 범죄 건수는?
    SELECT MAX(case_number)
    FROM crime_status
    WHERE police_station LIKE '강남' AND status_type LIKE '발생;
    ++ 데이터
    = SELECT police_station, crime_type, status_type, case_number
    FROM crime_status
    WHERE police_station LIKE '강남' AND status_type LIKE '발생;

- 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
    LIMIT 5;
    ++ 경찰서 종류를 검색 - DISTINCT를 사용하는 경우, ORDER BY(정렬)는 사용할 수 없음.
    SELECT DISTINCT police_station FROM crime_status LIMIT 5;
  • 예) 경찰서별로 총 발생 범죄 건수를 검색
    SELECT police_station, sum(case_number) 발생건수(#상단 타이틀 제목)
    FROM crime_status
    WHERE status_type LIKE '발생'
    GROUP BY police_station
    ORDER BY 발생건수 DESC
    LIMIT 5;
  • 예) 경찰서별로 평균 범죄 검거 건수를 검색
    SELECT police_station, AVG(case_number) 평균검거건수
    FROM crime_status
    WHERE status_type LIKE '검거'
    GROUP BY police_station
    ORDER BY 평균검거건수 DESC
    LIMIT 5;
  • 예) 경찰서별로 평균 범죄 발생 건수와 평균 범죄 검거 건수를 검색
    SELECT police_station, status_type, AVG(case_number)
    FROM crime_status
    GROUP BY police_station, status_type
    LIMIT 5;
  • 예) 경찰서 별 절도범죄 평균 발생건수를 가장 많은 건수 순으로 10개 검색
    select police_station, avg(case_number)
    from crime_status
    where crime_type = '절도' and status_type ='발생'
    group by police_station
    order by avg(case_number) desc
    limit 10;
  • 예) 경찰서 별 가장 많이 검거한 범죄건수를 가장 적은 순서대로 5개 나열
    select police_station, max(case_number)
    from crime_status
    where status_type ='검거'
    group by police_station
    order by max(case_number) asc
    limit 5;

- 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;
    ++ 확인
    SELECT sum(case_number)
    FROM crime_status
    WHERE status_type LIKE '발생' AND police_station LIKE '영등포'

  • 예) 경찰서별로 발생한 폭력과 절도의 범죄 건수 평균이 2000 이상인 경우를 탐색
    SELECT police_station, AVG(case_number)
    FROM crime_status
    WHERE (crime_type LIKE '폭력' OR crime_type LIKE '절도') AND status_type LIKE '발생
    GROUP BY police_station
    HAVING AVG(case_number) >= 2000;
    ++ 확인 1
    SELECT AVG(case_number)
    FROM crime_status
    WHERE (crime_type LIKE '폭력' OR crime_type LIKE '절도') AND status_type LIKE '발생 AND police_station LIKE '영등포'
    ++ 확인 2
    SELECT *
    FROM crime_status
    WHERE (crime_type LIKE '폭력' OR crime_type LIKE '절도') AND status_type LIKE '발생 AND police_station LIKE '영등포'

  • 예) 경찰서 별 가장 적게 검거한 건수 중 4건보다 큰 경우
    select police_station, min(case_number)
    from crime_status
    where status_type ='검거'
    group by police_station
    having min(case_number) > 4
    order by min(case_number) desc;

  • 예) '대문'으로 끝나는 이름의 경찰서별 범죄발생건수의 평균이 500건 이상인 경우
    select police_station, avg(case_number)
    from crime_status
    where police_station like '%대문' and status_type = '발생'
    group by police_station
    having avg(case_number) >= 500
    order by avg(case_number) desc;

0개의 댓글