開始使用免費開始

最長間隔

Evanston 的 311 需求單之間,最長的送出時間間隔是多少?

回顧 lead()lag() 的語法:

lag(column_to_adjust) OVER (ORDER BY ordering_column)
lead(column_to_adjust) OVER (ORDER BY ordering_column)

本練習屬於課程

SQL 中的探索式資料分析

檢視課程

練習說明

  • 使用 lead()lag()(視情況而定)選出本筆的 date_created,以及前一筆需求的 date_created
  • 計算每筆需求與前一筆需求之間的間隔。
  • 篩選出間隔最大的那一列。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

-- Compute the gaps
WITH request_gaps AS (
        SELECT date_created,
               -- lead or lag
               ___(date_created) OVER (___) AS previous,
               -- compute gap as date_created minus lead or lag
               date_created - ___(date_created) OVER (___) AS gap
          FROM evanston311)
-- Select the row with the maximum gap
SELECT *
  FROM request_gaps
-- Subquery to select maximum gap from request_gaps
 WHERE gap = (SELECT ___
                FROM request_gaps);
編輯並執行程式碼