集計

BigQuery入門

Matthew Forrest

Field CTO

BigQuery の集計

SELECT
  SUM(sales) AS total_sales,
  AVG(quantity) AS avg_quantity,
  MAX(price) AS max_price,
  MIN(price) AS min_price
FROM sales_data;
| total_sales | avg_quantity | max_price | min_price |
|-------------|--------------|-----------|-----------|
| 50          | 8            | 100       | 2         |
  • 大規模データを要約
  • 傾向とパターンを把握
  • BigQuery は集計に最適化
BigQuery入門

GROUP BY と ORDER BY

SELECT
  -- 後で 'order_id' 列でグループ化
  order_id,
  SUM(sales) AS order_total
FROM total_sales
-- ここで GROUP と ORDER を指定
GROUP BY order_id
ORDER BY order_total DESC;
| order_id | order_total |
|----------|-------------|
| 1        | 500         |
| 2        | 850         |
BigQuery入門

COUNT

SELECT
  category,
  -- 返された行数を COUNT
  COUNT(order_id) AS record_count
FROM total_sales
GROUP BY category;
| category    | record_count |
|-------------|--------------|
| shoes       | 238          |
| electronics | 183          |
BigQuery入門

SUM と AVG

SELECT
  category,
  SUM(cost) AS total_cost,
  AVG(cost) AS average_payment
FROM total_sales
GROUP BY category;
| category    | total_cost | avg_cost |
|-------------|------------|----------|
| shoes       | 10345      | 54       |
| electronics | 9340       | 34       |
BigQuery入門

MIN と MAX

SELECT
    MIN(product_photos_qty) as min_photo_count,
    MAX(product_photos_qty) as max_photo_count
FROM ecommerce.ecomm_products;
| min__photo_count | max__photo_count |
|----------|----------|
| 1    | 20 |
BigQuery入門

COUNTIF

SELECT
  category,
  -- cost が 500 を超える場合のみカウント
  COUNTIF(cost > 500) AS large_items
FROM total_sales
GROUP BY category;
| category    | large_items |
|-------------|-------------|
| shoes       | 2           |
| electronics | 35          |
BigQuery入門

HAVING

SELECT
category,
COUNT(order_id) as orders
FROM total_sales
-- 平均 cost が 75 を超えるカテゴリだけを条件に追加
HAVING AVG(cost) > 75;
| category    | orders |
|-------------|--------|
| shoes       | 25     |
| electronics | 98     |
BigQuery入門

ANY_VALUE

SELECT
  order_id,
  -- 任意の category を返す
  ANY_VALUE(category) as cat
  -- 最高 cost の category を返す
  ANY_VALUE(category HAVING MAX cost) AS max_cat
FROM total_sales
GROUP BY order_id;
| order_id | cat       | max_cat     |
|----------|-----------|-------------|
| 1        | shoes     | electronics |
| 2        | household | exercise    |
BigQuery入門

Ayo berlatih!

BigQuery入門

Preparing Video For Download...