문자형 데이터 타입과 흔한 문제

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,        -- 범주형 변수

       count(*)         -- 각 범주의 행 수 집계

  FROM product          -- 테이블

 GROUP BY category;     -- 범주형 변수


 category | count 
----------+-------
 Banana   |     1
 Apple    |     4
 apple    |     2
  apple   |     1
 banana   |     3
(5 rows)
SQL로 하는 탐색적 데이터 분석

정렬: 최빈값 우선

SELECT category,        -- 범주형 변수

       count(*)         -- 각 범주의 행 수 집계

  FROM product          -- 테이블

 GROUP BY category      -- 범주형 변수

 ORDER BY count DESC;   -- 최빈값부터 표시
 category | count 
----------+-------
 Apple    |     4
 banana   |     3
 apple    |     2
 Banana   |     1
  apple   |     1
(5 rows)
SQL로 하는 탐색적 데이터 분석

정렬: category 값

SELECT category,        -- 범주형 변수

       count(*)         -- 각 범주의 행 수 집계

  FROM product          -- 테이블

 GROUP BY category      -- 범주형 변수

 ORDER BY category;     -- 범주 기준 정렬
 category | count 
----------+-------
  apple   |     1
 Apple    |     4
 Banana   |     1
 apple    |     2
 banana   |     3
(5 rows)

SQL로 하는 탐색적 데이터 분석

알파벳 정렬

-- 결과

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

-- 알파벳 순서:

' ' < 'A' < 'a'
-- 결과에서 도출

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

SQL로 하는 탐색적 데이터 분석

흔한 문제

대소문자 구분

    'apple' != 'Apple'

 

공백도 포함

    ' apple' != 'apple'

    '' != '       '

빈 문자열은 null 아님

    '' != NULL

 

문장부호 차이

    'to-do' != 'to–do'

SQL로 하는 탐색적 데이터 분석

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

SQL로 하는 탐색적 데이터 분석

Preparing Video For Download...