ชนิดข้อมูลอักขระและปัญหาที่พบบ่อย

Exploratory Data Analysis ใน SQL

Christina Maimone

Data Scientist

ชนิดอักขระใน PostgreSQL

character(n) หรือ char(n)

  • ความยาวคงที่ n
  • ช่องว่างท้ายถูกละเว้นในการเปรียบเทียบ

character varying(n) หรือ varchar(n)

  • ความยาวแปรผัน สูงสุดไม่เกิน n

text หรือ varchar

  • ความยาวไม่จำกัด
Exploratory Data Analysis ใน 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...

Exploratory Data Analysis ใน 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)
Exploratory Data Analysis ใน 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)
Exploratory Data Analysis ใน 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)

Exploratory Data Analysis ใน 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'

Exploratory Data Analysis ใน SQL

ปัญหาที่พบบ่อย

ตัวพิมพ์เล็ก-ใหญ่มีความแตกต่าง

    'apple' != 'Apple'

 

ช่องว่างมีผล

    ' apple' != 'apple'

    '' != '       '

สตริงว่างไม่ใช่ null

    '' != NULL

 

ความแตกต่างของเครื่องหมายวรรคตอน

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

Exploratory Data Analysis ใน SQL

มาตรวจสอบข้อมูลข้อความกัน

Exploratory Data Analysis ใน SQL

Preparing Video For Download...