문자 데이터 유형과 주요 문제점

SQL을 활용한 탐색적 데이터 분석

Christina Maimone

Data Scientist

PostgreSQL 문자 유형

character(n) 또는 char(n)

  • 고정 길이 n
  • 비교 시 끝 공백 무시

character varying(n) 또는 varchar(n)

  • 변수 길이는 최대 n까지

text 또는 varchar

  • 길이 무제한
SQL을 활용한 탐색적 데이터 분석

텍스트 데이터 유형

범주형

Tues, Tuesday, Mon, TH

shirts, shoes, hats, pants

satisfied, very satisfied, unsatisfied

0349-938, 1254-001, 5477-651

red, blue, green, yellow

비정형 텍스트

I really like this product. I use it every day. It's my favorite color.

We've redesigned your favorite t-shirt to make it even better. You'll love...

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal...

SQL을 활용한 탐색적 데이터 분석

그룹화와 집계

SELECT category,        -- categorical variable

       count(*)         -- count rows for each category

  FROM product          -- table

 GROUP BY category;     -- categorical variable


 category | count 
----------+-------
 Banana   |     1
 Apple    |     4
 apple    |     2
  apple   |     1
 banana   |     3
(5 rows)
SQL을 활용한 탐색적 데이터 분석

정렬: 최빈값

SELECT category,        -- categorical variable

       count(*)         -- count rows for each category

  FROM product          -- table

 GROUP BY category      -- categorical variable

 ORDER BY count DESC;   -- show most frequent values first
 category | count 
----------+-------
 Apple    |     4
 banana   |     3
 apple    |     2
 Banana   |     1
  apple   |     1
(5 rows)
SQL을 활용한 탐색적 데이터 분석

정렬: category 값

SELECT category,        -- categorical variable

       count(*)         -- count rows for each category

  FROM product          -- table

 GROUP BY category      -- categorical variable

 ORDER BY category;     -- order by categorical variable
 category | count 
----------+-------
  apple   |     1
 Apple    |     4
 Banana   |     1
 apple    |     2
 banana   |     3
(5 rows)

SQL을 활용한 탐색적 데이터 분석

알파벳 순서

-- Results

 category | count 
----------+-------
  apple   |     1
 Apple    |     4
 Banana   |     1
 apple    |     2
 banana   |     3
(5 rows)

-- Alphabetical Order:

' ' < 'A' < 'a'
-- From results

' ' < 'A' < 'B' < 'a' < 'b'

SQL을 활용한 탐색적 데이터 분석

일반적인 문제

대소문자 구분

`'apple' != 'Apple'`

공백도 포함

`' apple' != 'apple'`

빈 문자열은 null이 아님

`'' != NULL`

구두점 차이

`'to-do' !=  'to–do'`
SQL을 활용한 탐색적 데이터 분석

텍스트 데이터를 살펴봅시다

SQL을 활용한 탐색적 데이터 분석

Preparing Video For Download...