반응형
MariaDB 쿼리
: 범위 별로 개수 구하기
2가지 방법 (sum & case 사용 방법과 count & case 사용방법)
-- 쿼리1 - sum / case 사용
select
sum(case when usage < 1 then 1 end) '1',
sum(case when 1 <= usage and usage < 10 then 1 end) '10',
sum(case when 10 <= usage and usage < 100 then 1 end) '100',
sum(case when usage > 100 then 1 end) '100+'
from
TABLE
-- 쿼리2 - count / case 사용
select
COUNT(case when age < 10 then 1 end) as '아이',
COUNT(case when 10 < age and age < 20 then 1 end) as '10대',
COUNT(case when 20 < age and age < 30 then 1 end) as '20대'
from
TABLE
반응형
'개발 > DB' 카테고리의 다른 글
[mySql, mariaDB] case 문 사용 방법 (0) | 2022.04.06 |
---|---|
(Mybatis) Where 1=1 처럼 동적으로, update set 컬럼을 바꾸고 싶을때 (0) | 2022.03.22 |
(펌) [DBMS] [MySQL] COUNT의 잘못된 인식과 속도차이 (0) | 2022.01.18 |
Oracle 특정일자부터 오늘까지 차이 계산하는 쿼리 (4) | 2021.06.12 |
[DB] 최적화된 Query 작성을 위한 기본적인 팁 (for. 쿼리 초보자) (0) | 2021.05.15 |