구분자로 나누어 입력되어 있는 난감한 데이터의 처리 > db

본문 바로가기
사이트 내 전체검색

db

구분자로 나누어 입력되어 있는 난감한 데이터의 처리

페이지 정보

작성자 서방님 댓글 0건 조회 116회 작성일 07-10-16 15:56

본문

구분자로 keyword 를 나눈 DATA 컬럼의
중복을 제거하면서 가장 많은 keyword 순서대로
DATA 를 나열해보도록 하겠습니다.

최초에 모델링이 잘못되었을때 이런방식으로 query 문을
작성하게 되면 해결됩니다. (사실, 이런식으로 모델을 했다는것
자체가 잘못된 겁니다만 실무에서는 이렇게 구분자를 이용해서
모델링을 해놓은 PHYSICAL MODEL 이 너무 많습니다. )


***문제의 user_keyword 내용***

user        user_keyword
----       -----------------------------------
홍길동    영화/음악/인타넷/성/결혼/사랑
길동이    영화/음악/자동차/사진/카메라/오토바이
삼식이    대학/요가/헬스/드라마/소설/연예인
만사마    ZARD/아야세하루카/영화/일드/대전/운동
.
.
.
.
.
----------------------------------------------
위와 같이 사용자 별로 구분자 '/' 를 이용해
사용자 취향이 무엇인지 구분해놨습니다.
이럴경우 사용자들이 어떤 keyword 를 가장 많이 선택했는지 select 한다는게
조금 난감할수도 있습니다.
이런 형태로 입력되어 있는 DATA 를
사용자가 선택한 keyword 종류별, count 를 아래와 같은 형태로 select 해보겠습니다.

user_keyword   count
------------   ----
영화  386
음악  348
사랑  189
게임  143
여행  139
패션  127
소설  91
만화  67
컴퓨터  60
친구  54
연예인  44
운동  43
사진  34
판타지  33
재희  33
애니  32
인터넷  28
스포츠  26
.
.
.
.
.

이런식으로 select 해보겠습니다.


select c_1.s_key,count(*) as cnt
from
(select
SubString(SubString(user_keyword,0,40 ),1
,CharIndex('/',SubString(user_keyword,0,40)) - 1) s_key
from User_table where user_keyword is not null
union all
select
SubString(SubString(user_keyword,CharIndex('/',user_keyword,0) + 1,40 ),1
,CharIndex('/',SubString(user_keyword,CharIndex('/',user_keyword,0) + 1,40)) - 1) s_key
from User_table where user_keyword is not null
union all
select
SubString(SubString(user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,0) + 1) + 1,40 ),1
,CharIndex('/',SubString(user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,0) + 1) + 1,40)) - 1) s_key
from User_table where user_keyword is not null
union all
select
SubString(SubString(user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,0) + 1) + 1) + 1,40 ),1
,CharIndex('/',SubString(user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,0) + 1) + 1) + 1,40)) - 1) s_key
from User_table where user_keyword is not null
union all
select
SubString(SubString(user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,0) + 1) + 1) + 1) + 1,40 ),1
,CharIndex('/',SubString(user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,0) + 1) + 1) + 1) + 1,40)) - 1) s_key
from User_table where user_keyword is not null
union all
select
SubString(SubString(user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,0) + 1) + 1) + 1) + 1) + 1,40 ),1
,CharIndex('/',SubString(user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,CharIndex('/',user_keyword,0) + 1) + 1) + 1) + 1,40)) - 1) s_key
from User_table where user_keyword is not null) as c_1
where c_1.s_key is not null  and c_1.s_key!=''
group by c_1.s_key
order by cnt desc

 

쿼리의 양은 많지만
내용은 매우 간단합니다.

charindex 를 통해 '/' 로 구분해놓은 글자위치를 숫자로 반환하면서
동시에 substring 을 이용해서 user_keyword 의 값을 리턴해주는 방식이다.

구분자로 나누어 놓거나 혹은 특수문자로 나누어 놓은
컬럼이 있을경우 위와 같은 방식을 이용해서
원하는 조합을 얻어내는데 조금이나마 도움이 될까해서 올려봤습니다.

댓글목록

등록된 댓글이 없습니다.

Total 464건 22 페이지
db 목록
번호 제목 글쓴이 조회 날짜
149 서방님 104 10-16
148 서방님 180 10-16
147 서방님 181 10-16
열람중 서방님 117 10-16
145 서방님 178 10-16
144 서방님 153 10-16
143 서방님 151 10-16
142 서방님 169 10-16
141 서방님 132 10-16
140 서방님 933 10-16
139 서방님 149 10-16
138 서방님 180 10-16
137 서방님 164 10-16
136 서방님 178 10-15
135 서방님 178 09-21
게시물 검색

회원로그인

접속자집계

오늘
29
어제
302
최대
1,347
전체
155,090
Latest Crypto Fear & Greed Index

그누보드5
Copyright © 서방님.kr All rights reserved.