[실무에서 바로 쓰는 SQL 기본과 SQL 튜닝][ORACLE] 25강

MinHee·2023년 5월 20일
0
post-thumbnail

빅데이터 분석

우리나라 남자들이 가장 많이 걸리는 암?

select *
from cancer
where 성 != "남녀전체" and 발생연도='1999' and 발생자수 is not null and 암종 != '모든암' and 성='남자'
order by 발생자수 desc fetch first 4 rowd only;

연설문을 오라클 테이블에 문장별로 저장

SELECT REGEXP_SUBSTR('I NEVER GRADUATED FROM COLLEGE', '[^ ]+', 1, 2) word
from dual;

해당 문장에서 공백이 아닌 어절 여러개([^ ])를 검색해서, 그중 첫번째부터 검색해서 두번째에 나오는 것을 출력

출력문을 어절 단위로 출력

select regexp_substr(lower(speech_text), '[^ ]+', 1, a) word
from speech, (select level a
from dual
connect by level <=52);

테이블(연설문) 전체 데이터 중에서 가장 많이 나온 단어 순서로 출력

select word, count(*)
from (
select regexp_substr(lower(speech_text), '[^ ]+', 1, a) word
from speech, (select level a
from dual
connect by level <=52)

  )

where word is not null
group by word
order by count(*) desc;

스피치 연설문 테이블을 어절로 나눈 결과를 뷰로 생성함

create or replace view speech_view
as
select regexp_substr(lower(speech_text), '[^ ]+', 1, a) word
from speech, (select level a
from dual
connect by level <=52);

스피치 연설문 중에서 긍정단어와 일치하는 단어의 개수를 출력

select count(word) as 긍정단어
from speech_view
where lower(word) in (select lower(p_text)
from positive);

select *
from crime_day
unpivot (cnt for day_cnt in (sun_cnt, mon_cnt, tue_cnt, wed_cnt, thu_cnt, fri_cnt, sat_cnt));

unpivot : 칼럼과 행이 변환됨

절도 범죄에 대해서 가장 많이 발생한 요일별로 순위매겨서 출력

select day_cnt, cnt, rank() over (order by cnt desc) rnk
from crime_day_unpivot
where trim(crime_type) = '절도';

1위인 칼럼만 출력하고 싶으니, from절의 서브쿼리로 넣어야 함

select *
from (select day_cnt, cnt, rank() over (order by cnt desc) rnk
from crime_day_unpivot
where trim(crime_type) = '절도'
)
where rnk = 1;

대학등록금이 비싼 순서대로 순위를 매겨서 출력

select university, tuition_fee, rank() over (order by tuition_fee desc nulls last) 순위
from university_fee;

select *
from (
select university, tuition_fee,
rank() over (order by tuition_fee desc nulls last) 순위
from university_fee;
)
where 순위 = 1;

profile
성장하는 개발자

0개의 댓글