MySQL Aggregate Functions (집계함수) 2

dpwl·2024년 4월 10일
0

Data Analysis

목록 보기
23/83

Aggregate Functions (집계함수)COUNTSUM여기서 자세히 알아볼 수 있다.

5. AVG

AVG숫자 column의 평균을 계산해주는 함수다.

AVG 문법

SELECT AVG(column)
FROM tablename
WHERE condition;

Example 1: 평균 폭력 검거 건수를 구하기

select avg(case_number) from crime_status
where crime_type='폭력' and status_type='검거';

  • 확인해보기
select police_station, crime_type, status_type, case_number
from crime_status
where crime_type='폭력' and status_type='검거';

Example 2: 중부경찰서 범죄 평균 발생 건수를 구하기

select avg(case_number)
from crime_status
where police_station='중부' and status_type='발생';

  • 확인해보기
select police_station, crime_type, status_type, case_number
from crime_status
where police_station='중부' and status_type='발생';

6. MIN

MIN숫자 column 중 가장 작은 값을 찾아주는 함수다.

MIN 문법

SELECT MIN(column)
FROM table
WHERE condition;

Example 1: 강도 발생 건수가 가장 적은 경우 몇건인지 찾기

select min(case_number)
from crime_status
where crime_type='강도' and status_type='발생';

  • 확인해보기
select police_station, crime_type, status_type, case_number
from crime_status
where crime_type='강도' and status_type='발생';

Example 2: 중부경찰서에서 가장 낮은 검거 건수 찾기

select min(case_study)
from crime_status
where police_station='중부' and status_type='검거';

  • 확인해보기
select police_station, crime_type, status_type, case_number
from crime_status
where police_station='중부' and status_type='검거';

7. MAX

MAX숫자 column 중 가장 큰 값을 찾아주는 함수다.

MAX 문법

SELECT MAX(column)
FROM tablename
WHERE condition;

Example 1: 살인이 가장 많이 검거된 건수 찾기

select max(case_number)
from crime_status
where crime_type='살인' and status_type='검거';

  • 확인해보기
select police_station, crime_type, status_type, case_number
from crime_status
where crime_type='살인' and status_type='검거';

Example 2: 강남 경찰서에서 가장 많이 발생한 범죄 건수 찾기

select max(case_number)
from crime_status
where police_station='강남' and status_type='발생';

  • 확인해보기
select police_station, crime_type, status_type, case_number
from crime_status
where police_station='강남' and status_type='발생';

profile
거북선통통통통

0개의 댓글