데이터베이스 개발자 질문과 답변 게시판
데이터베이스 개발/운영 관련 질문과 답변을 올리는 게시판입니다. 궁금하신 내용을 이곳에서 문의하시면 SQLER 분들의 답변을 받으실 수 있습니다. 문의를 하실때에는 최근 작업하신 특이 사항이나, 장애 발생 전 상황을 상세히 올려 주시면 답글을 적어주시는 SQLER분들의 답변이 더 정확할 수 있으니 도움 되시길 바랍니다. 쿼리 문의일 경우, 실제 문제가 재현되는 테이블생성, 샘플데이터 생성 쿼리를 함께 올려 주시면 더 빠르고 정확한 쿼리 문의 응답이 가능합니다.
안녕하세요...
regno 주민등록번호 필드 연령별 성별을 뽑으려고 합니다...
필드데이터
regno
790111-2321234
이러한 형태로 데이터 들어가 있구요...
아래처럼 쿼리 할려고 합니다...
연령 계 남자 여자
20대 이하 200 150 50
30대 300 250 50
40대 210 200 10
50대 410 390 20
60대 이상 200 190 10
이렇게 뽑으려고 합니다... 어떻게 하나요?
Comment 1
-
건우아빠
2009.11.27 16:42
with result
as (
select jumin
, DATEDIFF(yy, (case when substring(jumin,8,1) > 2 then '20'+substring(jumin,1,2) else '19'+substring(jumin,1,2) end ), convert(varchar(4),getdate(),112)) + 1 year
, case convert(integer , substring(jumin,8,1)) % 2 when 0 then '여' else '남' end sex
from ( select '790111-2321234' jumin union all
select '700111-1321234' jumin union all
select '750111-1321234' jumin union all
select '820111-1321234' jumin union all
select '930111-2321234' jumin union all
select '560111-1321234' jumin union all
select '010111-2321234' jumin union all
select '050111-3321234' jumin union all
select '040111-4321234' jumin union all
select '090111-4321234' jumin ) a )
select yeard
, sum( case when sex = '남' then 1 else 0 end) [남]
, sum( case when sex = '여' then 1 else 0 end) [여]
from ( select (case when year / 10 * 10 <= 20 then 20 else ( case when year / 10 * 10 >= 60 then 60 else year / 10 * 10 end ) end ) yeard
, sex
from result ) r
group by yeard