집계

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,
  -- 비용이 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
-- 평균 비용이 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 입문

연습해 봅시다!

BigQuery 입문

Preparing Video For Download...