จัดกลุ่มและแปลงรหัสค่าข้อมูล
ตาราง evanston311.category มีค่าที่แตกต่างกันเกือบ 150 ค่า แต่บางหมวดหมู่มีรูปแบบคล้ายกัน คือ "หมวดหมู่หลัก - รายละเอียด" หากรวมข้อมูลตามหมวดหมู่หลัก จะช่วยให้เข้าใจได้ดีขึ้นว่าคำขอประเภทใดพบบ่อยที่สุด
ให้สร้างตารางชั่วคราว recode เพื่อแมปค่า category ที่ไม่ซ้ำกันไปยังค่า standardized ใหม่ โดยให้ค่า standardized เป็นส่วนของหมวดหมู่ก่อนเครื่องหมายขีดกลาง ('-') ซึ่งสามารถดึงค่านี้ออกมาได้ด้วยฟังก์ชัน split_part():
split_part(string text, delimiter text, field int)
นอกจากนี้ยังต้องทำความสะอาดข้อมูลเพิ่มเติมสำหรับบางกรณีที่ไม่ตรงกับรูปแบบนี้
จากนั้นจึงนำตาราง evanston311 มา JOIN กับ recode เพื่อจัดกลุ่มคำขอตามค่า standardized ใหม่
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Exploratory Data Analysis ใน SQL
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
-- Fill in the command below with the name of the temp table
DROP TABLE IF EXISTS ___;
-- Create and name the temporary table
CREATE ___ ___ ___ AS
-- Write the select query to generate the table with distinct values of category and standardized values
SELECT DISTINCT category,
___(___(___, ___, ___)) AS standardized
-- What table are you selecting the above values from?
FROM ___;
-- Look at a few values before the next step
SELECT DISTINCT standardized
FROM recode
WHERE standardized LIKE 'Trash%Cart'
OR standardized LIKE 'Snow%Removal%';