字元型別與常見問題

SQL 中的探索式資料分析

Christina Maimone

Data Scientist

PostgreSQL 字元型別

character(n)char(n)

  • 固定長度 n
  • 比較時忽略結尾空白

character varying(n)varchar(n)

  • 變動長度,最大至 n

textvarchar

  • 長度不設限
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 中的探索式資料分析

排序:類別值

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...