This commit is contained in:
Greg Finley
2023-03-23 12:02:17 -07:00
parent 442495d719
commit a0528bed69
2 changed files with 38 additions and 0 deletions

View File

@@ -490,6 +490,20 @@ class PragmaStatementSegment(BaseSegment):
)
class OverClauseSegment(ansi.OverClauseSegment):
"""An OVER clause for window functions."""
match_grammar: Matchable = Sequence(
"OVER",
OneOf(
Ref("SingleIdentifierGrammar"), # Window name
Bracketed(
Ref("WindowSpecificationSegment", optional=True),
),
),
)
class StatementSegment(ansi.StatementSegment):
"""Overriding StatementSegment to allow for additional segment parsing."""

View File

@@ -1 +1,25 @@
SELECT a FROM foo LIMIT 10;
WITH time_cte AS (
SELECT
branch,
created_at,
time,
cast(time - LAG (time, 1, time) OVER (ORDER BY time) as real) AS time_spent
FROM heartbeats h
WHERE user_id = ? AND created_at >= DATE('now', 'start of day')
ORDER BY id
LIMIT -1 OFFSET 1
)
SELECT
branch as name,
cast(time_spent as real) as time_spent,
cast(time_spent / (SELECT SUM(time_spent) FROM time_cte) as real) as time_percentage
FROM (
SELECT
branch,
cast(SUM(time_spent) as real) AS time_spent
FROM time_cte
GROUP BY branch
ORDER BY time_spent DESC
);