按星期幾的變化
完成一個請求所需的時間,會不會隨著建立該請求的星期幾而不同?
我們可以把時間戳轉成字元資料來取得星期幾的名稱:
to_char(date_created, 'day')
但星期幾的文字名稱會以字母順序排序,而不是時間先後。若要依時間先後取得每一天對應的整數值,可以使用:
EXTRACT(DOW FROM date_created)
DOW 代表「day of week(星期幾)」。
本練習屬於課程
SQL 中的探索式資料分析
練習說明
- 選取建立請求(
date_created)時的星期幾名稱為day。 - 選取請求完成時間(
date_completed)與建立時間之間的平均時長為duration。 - 依照
day(星期幾名稱)以及星期幾的整數值(請使用函式)進行分組。 - 以與
GROUP BY相同的函式,依星期幾的整數值進行排序。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- Select name of the day of the week the request was created
SELECT ___(___, ___) AS day,
-- Select avg time between request creation and completion
___(___ - ___) AS duration
FROM evanston311
-- Group by the name of the day of the week and
-- integer value of day of week the request was created
GROUP BY day, EXTRACT(___ ___ ___)
-- Order by integer value of the day of the week
-- the request was created
ORDER BY EXTRACT(___ ___ ___);