본문 바로가기

공부 자료/SQL

[25일차] 실전 데이터 분석 : 우리집 주변은 안전할까?!-3

- 쿼리 앞에 CREATE TABLE 테이블명 AS를 입력하면 쿼리를 바탕으로 한 새로운 데이터 테이블이 생김

- TO_CHAR(수치형 데이터, '999,999')는 숫자의 천의 자리에 쉼표를 찍어줌

 

select case when round(a.전체카메라설치수/(a.전체카메라설치수+b.설치대수)*100, 2) <= 50 then '설치미비'
           when round(a.전체카메라설치수/(a.전체카메라설치수+b.설치대수)*100, 2) <= 80 then '설치보통'
           when round(a.전체카메라설치수/(a.전체카메라설치수+b.설치대수)*100, 2) <= 95 then '설치우수'
           when round(a.전체카메라설치수/(a.전체카메라설치수+b.설치대수)*100, 2) > 95 then '완전설치'
           else 'chk' end as 설치구분
        , to_char(sum(a.전체카메라설치수 + b.설치대수),'999,999') as 전체설치건수
        , to_char(round(avg(a.전체카메라설치수 + b.설치대수)),'999,999') as 평균건수
        , COUNT(*) as 지역수
        , to_char(sum(a.전체카메라설치수),'999,999') as 카메라수
        , to_char(sum(b.설치대수),'999,999') as 비상벨수
        , round(sum(a.전체카메라설치수)/sum(a.전체카메라설치수 + b.설치대수)*100,2)||'%' as 카메라설치비율
        , round(sum(b.설치대수)/sum(a.전체카메라설치수 + b.설치대수)*100,2)||'%' as 비상벨설치비율
from cctv설치현황 a inner join 비상벨설치현황 b
on a.지역 = b.지역
group by case when round(a.전체카메라설치수/(a.전체카메라설치수+b.설치대수)*100, 2) <= 50 then '설치미비'
           when round(a.전체카메라설치수/(a.전체카메라설치수+b.설치대수)*100, 2) <= 80 then '설치보통'
           when round(a.전체카메라설치수/(a.전체카메라설치수+b.설치대수)*100, 2) <= 95 then '설치우수'
           when round(a.전체카메라설치수/(a.전체카메라설치수+b.설치대수)*100, 2) > 95 then '완전설치'
           else 'chk' end
order by 카메라설치비율 desc;

 

25일차 - 츨석 인증