Your company has several retail locations. Your company tracks the total number of sales made at each location each day. You want to use SQL to calculate the weekly moving average of sales by location to identify trends for each store. Which query should you use?
A)

B)

C)

D)

To calculate the weekly moving average of sales by location:
The query must group by store_id (partitioning the calculation by each store).
The ORDER BY date ensures the sales are evaluated chronologically.
The ROWS BETWEEN 6 PRECEDING AND CURRENT ROW specifies a rolling window of 7 rows (1 week if each row represents daily data).
The AVG(total_sales) computes the average sales over the defined rolling window.
Chosen query meets these requirements:
Extract from Google Documentation: From 'Analytic Functions in BigQuery' (https://cloud.google.com/bigquery/docs/reference/standard-sql/analytic-function-concepts): 'Use ROWS BETWEEN n PRECEDING AND CURRENT ROW with ORDER BY a time column to compute moving averages over a fixed number of rows, such as a 7-day window, partitioned by a grouping key like store_id.' Reference: Google Cloud Documentation - 'BigQuery Window Functions' (https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls).
Currently there are no comments in this discussion, be the first to comment!