Skip to content

Instantly share code, notes, and snippets.

@Mr--John-Doe
Last active January 14, 2022 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mr--John-Doe/633608fefc6a90105aac6f321bed9624 to your computer and use it in GitHub Desktop.
Save Mr--John-Doe/633608fefc6a90105aac6f321bed9624 to your computer and use it in GitHub Desktop.
WITH prep AS (
SELECT *
, LAG(accident_ts) OVER (PARTITION BY severity ORDER BY accident_ts) AS previous_accident_ts
, DATEDIFF(HOUR, previous_accident_ts, accident_ts) AS time_between_accidents
FROM temp.CAMBRIDGESHIRE_ACCIDENTS
)
SELECT severity
, AVG(time_between_accidents) AS avg_time_between_accidents_by_severity
FROM prep
GROUP BY severity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment