오라클 쿼리 질문드립니다. 0 2 1,419

by 지에나르 [SQL Query] [2017.06.26 13:39:18]


주소 성별 나이대
경기도 수원 10대
경기도 수원 10대
경기도 화성 30대
경기도 성남 50대

위와같은 테이블이 있습니다!

이걸 주소별로 성별, 나이대 별로 결과를 뽑고싶습니다!

주소 10대남자 20대남자 30대남자 40대남자 50대남자 60이상 남자 10대여자 20대여자 30대여자 40대여자 50대여자 60이상 여자    
경기도 수원 1 0 0 0 0 0 1 0 0 0 0 0    
경기도 화성 0 0 0 0 0 0 0 0 1 0 0 0    
경기도 성남 0 0 0 0 1 0 0 0 0 0 0 0    

이런식으로 주소별로 남자 10대 몇명 20대몇명 ....

여자 10대몇명 20대몇명... 이런식으로 뽑고싶은데 쿼리를 어떻게 짜야 될까요?

by 知音 [2017.06.26 14:40:59]
with tbl as (
     select '경기도 수원' addr, '남' sex, '10대' age from dual union all
     select '경기도 수원' addr, '여' sex, '10대' age from dual union all
     select '경기도 화성' addr, '여' sex, '30대' age from dual union all
     select '경기도 성남' addr, '남' sex, '50대' age from dual
)   
select addr
     , count(case when age = '10대' and sex = '남' then 1 end) male_10
     , count(case when age = '20대' and sex = '남' then 1 end) male_20
     , count(case when age = '30대' and sex = '남' then 1 end) male_30
     , count(case when age = '40대' and sex = '남' then 1 end) male_40
     , count(case when age = '50대' and sex = '남' then 1 end) male_50
     , count(case when age = '60대' and sex = '남' then 1 end) male_60
     , count(case when age = '10대' and sex = '여' then 1 end) female_10
     , count(case when age = '20대' and sex = '여' then 1 end) female_20
     , count(case when age = '30대' and sex = '여' then 1 end) female_30
     , count(case when age = '40대' and sex = '여' then 1 end) female_40
     , count(case when age = '50대' and sex = '여' then 1 end) female_50
     , count(case when age = '60대' and sex = '여' then 1 end) female_60
  from tbl
 group by addr
 order by addr
  

 


by 머털이 [2017.06.26 15:53:43]
with t0 as (
select '경기도 수원' addr , '남' sex, '10대' ages from dual
union all
select '경기도 수원' addr , '여' sex, '10대' ages from dual
union all
select '경기도 화성' addr , '여' sex, '30대' ages from dual
union all
select '경기도 성남' addr , '남' sex, '50대' ages from dual
)
select addr
, sum(case when (sex = '남' and ages = '10대') then 1 else 0 end) "10대남자"
, sum(case when (sex = '남' and ages = '20대') then 1 else 0 end) "20대남자"
, sum(case when (sex = '남' and ages = '30대') then 1 else 0 end) "30대남자"
, sum(case when (sex = '남' and ages = '40대') then 1 else 0 end) "40대남자"
, sum(case when (sex = '남' and ages = '50대') then 1 else 0 end) "50대남자"
, sum(case when (sex = '남' and to_number(replace(ages, '대', '')) >= 60) then 1 else 0 end) "60이상 남자"
, sum(case when (sex = '여' and ages = '10대') then 1 else 0 end) "10대여자"
, sum(case when (sex = '여' and ages = '20대') then 1 else 0 end) "20대여자"
, sum(case when (sex = '여' and ages = '30대') then 1 else 0 end) "30대여자"
, sum(case when (sex = '여' and ages = '40대') then 1 else 0 end) "40대여자"
, sum(case when (sex = '여' and ages = '50대') then 1 else 0 end) "50대여자"
, sum(case when (sex = '여' and to_number(replace(ages, '대', '')) >= 60) then 1 else 0 end) "60이상 여자"
from t0
group by addr
;


with t0 as (
select '경기도 수원' addr , '남' sex, '10대' ages from dual
union all
select '경기도 수원' addr , '여' sex, '10대' ages from dual
union all
select '경기도 화성' addr , '여' sex, '30대' ages from dual
union all
select '경기도 성남' addr , '남' sex, '50대' ages from dual
)
select * from (
select addr, ages||sex as_col
from t0)
pivot (count(1) for as_col in ('10대남', '20대남', '30대남', '40대남', '50대남', '60대남'
                              ,'10대여', '20대여', '30대여', '40대여', '50대여', '60대여'))

 

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입