Timedora
Free Online Flashcard Deck

SQL Time-Series Window Functions Free Online Flashcards

Study SQL Time-Series Window Functions with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.

12 cards
01
Front

When should timestamps be bucketed into business dates?

Back

Bucket timestamps into business dates in the chosen reporting time zone before calculating daily metrics. PostgreSQL’s date_trunc can truncate timestamps to a chosen unit.

02
Front

How can SQL retrieve a previous period’s value for comparison?

Back

Use LAG with an ordering column to retrieve the previous row’s value. Expose that result in a later CTE or query layer before using it in another calculation.

03
Front

When does a seven-row ROWS frame represent seven calendar days?

Back

ROWS BETWEEN 6 PRECEDING AND CURRENT ROW includes at most seven physical rows. It represents seven consecutive calendar days only when there is exactly one row per calendar day.

04
Front

How do window aggregates calculate rolling metrics without collapsing dates?

Back

Apply a window aggregate over an explicitly defined, ordered frame. This calculates rolling measures while retaining one output row per date.

05
Front

How do you prepare daily data when zero-activity dates should count?

Back

Aggregate records to the reporting grain first. If zero-activity dates should count, create a complete calendar, left join the data, and convert missing activity to zero.

06
Front

What does RANGE measure in a SQL window frame?

Back

RANGE measures distance in the ordering value rather than counting rows. In PostgreSQL, an offset RANGE frame for date or timestamp ordering uses an interval and requires a single ORDER BY expression.

07
Front

Which aggregates can use a seven-row rolling frame?

Back

SUM, AVG, and COUNT can use ROWS BETWEEN 6 PRECEDING AND CURRENT ROW to calculate measures over the current row and up to six preceding rows.

08
Front

How can percentage change use LAG safely?

Back

Put the LAG result in a later CTE or query layer, then use NULLIF(previous_day_revenue, 0) as the percentage-change denominator to avoid division by zero.

09
Front

When should you filter report dates for a rolling metric?

Back

Include enough dates before the report start to complete the first rolling window. Calculate the metric over that expanded range, then filter to the displayed report dates.

10
Front

Why might UTC dates be unsuitable for daily business metrics?

Back

Use the chosen reporting time zone to assign timestamps to business-date buckets before calculating daily metrics. UTC dates may not match local business dates.

11
Front

What does LAG(revenue) OVER (ORDER BY day) return?

Back

LAG(revenue) OVER (ORDER BY day) retrieves the revenue from the previous row in day order, which can be used to compare consecutive days.

12
Front

What does filling missing dates with zero do to a rolling average?

Back

A calendar and left join can fill dates without orders; converting their revenue to zero makes those dates part of the rolling average. For active days only, define a separate counting rule.