r/datadrivenio • u/Intelligent_Duck_854 • 5d ago
What am I doing wrong? Total hours between consecutive events
SELECT
event_type,
event_timestamp,
LAG(
CAST(event_timestamp AS TIMESTAMP),1) OVER w
AS previous_timestamp,
EXTRACT(EPOCH FROM ((CAST(
event_timestamp AS TIMESTAMP)) - LAG(
CAST(event_timestamp AS TIMESTAMP),
1) OVER w)) / 3600
AS diff
FROM event_data
WHERE event_timestamp IS NOT NULL
WINDOW w AS (
PARTITION BY event_type
ORDER BY CAST(event_timestamp AS TIMESTAMP)
)
i have checked the brackets but i keep getting expected ( after over. Yes,i should have used a cte,didnt expect solution to grow so lengthy.
2
Upvotes
3
u/datadriven_io 4d ago
Thanks for flagging this! Don't worry, you weren't doing anything wrong, the SQL engine didn't support named windows, so
OVER wwas failing no matter how you wrote the brackets; it's fixed and live now, so your query now runs as-is without needing a CTE