Introduction à BigQuery
Matthew Forrest
Field CTO


SELECT customer_id, order_date, order_total, ROW_NUMBER() OVER(PARTITION BY customer_idORDER BY order_date) AS order_sequence FROM orders;
ROW_NUMBER(): Fonction window qui renvoie le numéro de ligneOVER(): Définit la fenêtrePARTITION BY customer_id: Regroupe par clientORDER BY order_date: Trie les données à l'intérieur de chaque partitionorder_sequence: Résultat de l'opération windowSELECT
product_id,
product_photos_qty,
-- Ordinal rank for each row
RANK() OVER(
ORDER BY product_photos_qty DESC
) as rank,
-- Percentile rank for each row
PERCENT_RANK() OVER(
ORDER BY product_photos_qty
) as percent
FROM ecommerce.ecomm_products
ORDER BY product_photos_qty DESC;

SELECT
product_id,
-- Returns value from previous row
LAG(product_photos_qty) OVER(
ORDER BY product_photos_qty
) as lag,
product_photos_qty,
-- Returns value from next row
LEAD(product_photos_qty) OVER(
ORDER BY product_photos_qty
) as lead
FROM ecommerce.ecomm_products
ORDER BY product_photos_qty DESC;

SELECT
order_id,
order_timestamp,
SUM(cost) OVER(
ORDER BY order_timestamp
ROWS BETWEEN 2 PRECEDING
AND CURRENT ROW) as rolling_avg
FROM sales_data
ORDER BY order_timestamp
Options de délimitation par lignes :
UNBOUNDED PRECEDING: Toutes les lignes avantUNBOUNDED FOLLOWING: Toutes les lignes après[INT] ROWS PRECEDING: Nombre précis de lignes avant[INT] ROWS FOLLOWING: Nombre précis de lignes aprèsSELECT
product_id,
product_photos_qty,
RANK() OVER(
ORDER BY product_photos_qty DESC
) as rank
FROM ecommerce.ecomm_products
-- Filter using QUALIFY
QUALIFY rank < 4
ORDER BY product_photos_qty DESC;

HAVING car il n'y a pas d'agrégationIntroduction à BigQuery