ここでは、ecommerce データセットを使用します。このデータセットには、Google Merchandise Store に関する数百万件の Google アナリティクス レコードが含まれており、BigQuery に読み込まれています。このデータセットのコピーを使用して、フィールドや行からどのような分析情報が得られるのかを確認します。
#standardSQL
# SKU が複数ある商品を特定する
SELECT
DISTINCT
COUNT(DISTINCT productSKU) AS SKU_count,
STRING_AGG(DISTINCT productSKU LIMIT 5) AS SKU,
v2ProductName
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE productSKU IS NOT NULL
GROUP BY v2ProductName
HAVING SKU_count > 1
ORDER BY SKU_count DESC
# 商品名が一意ではない(バリエーションの場合)
[実行] をクリックします。
結果:
複数の SKU を持つ商品名はありますか。クエリの結果を確認してください。
解答: はい
1 つの商品名が複数の SKU に関連付けられている場合もあります。商品のバリエーションによってこのようになることがあります。たとえば、1 つの商品名(例: T シャツ)に対して、色やサイズなど、複数の商品バリエーションがある場合があります。つまり、1 つの商品に多数の SKU があることを想定できます。
以下のクエリをコピーして貼り付け、確認します。
#standardSQL
SELECT
DISTINCT
COUNT(DISTINCT v2ProductName) AS product_count,
STRING_AGG(DISTINCT v2ProductName LIMIT 5) AS product_name,
productSKU
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE v2ProductName IS NOT NULL
GROUP BY productSKU
HAVING product_count > 1
ORDER BY product_count DESC
# SKU が一意ではない(データ品質に問題がある)
#standardSQL
SELECT DISTINCT
website.v2ProductName,
website.productSKU,
inventory.stockLevel
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
JOIN `data-to-insights.ecommerce.products` AS inventory
ON website.productSKU = inventory.SKU
WHERE productSKU = 'GGOEGPJC019099'
#standardSQL
SELECT
productSKU,
SUM(stockLevel) AS total_inventory
FROM (
SELECT DISTINCT
website.v2ProductName,
website.productSKU,
inventory.stockLevel
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
JOIN `data-to-insights.ecommerce.products` AS inventory
ON website.productSKU = inventory.SKU
WHERE productSKU = 'GGOEGPJC019099'
)
GROUP BY productSKU
#standardSQL
SELECT
COUNT(DISTINCT website.productSKU) AS distinct_sku_count
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
解答: ウェブサイトのデータセットから、重複を排除した SKU が 1,909 件取得されます。
結合における問題: 結合後にデータのレコードが失われる
ここでは、商品在庫のデータセットに対してもう一度結合します。
以下のクエリをコピーして貼り付けます。
#standardSQL
SELECT DISTINCT
website.productSKU
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
JOIN `data-to-insights.ecommerce.products` AS inventory
ON website.productSKU = inventory.SKU
#standardSQL
# 両方のテーブルから ID フィールドを取得
SELECT DISTINCT
website.productSKU AS website_SKU,
inventory.SKU AS inventory_SKU
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
JOIN `data-to-insights.ecommerce.products` AS inventory
ON website.productSKU = inventory.SKU
# 両方のテーブルに存在する ID を取得。ここからどのように分析を進めるか?
#standardSQL
# 結合のタイプが重要
# 両方のテーブルから ID フィールドを取得する
SELECT DISTINCT
website.productSKU AS website_SKU,
inventory.SKU AS inventory_SKU
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
LEFT JOIN `data-to-insights.ecommerce.products` AS inventory
ON website.productSKU = inventory.SKU
[実行] をクリックします。
左結合を適切に使用し、元のウェブサイトの SKU 1,909 件がすべて結果に返されました。
商品在庫のセットから何件の SKU が失われましたか。
クエリを作成して、在庫テーブルの NULL 値を検索条件に指定します。
解答例:
#standardSQL
# ウェブサイトのテーブルにはあって商品在庫テーブルにはない商品 SKU を探す
SELECT DISTINCT
website.productSKU AS website_SKU,
inventory.SKU AS inventory_SKU
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
LEFT JOIN `data-to-insights.ecommerce.products` AS inventory
ON website.productSKU = inventory.SKU
WHERE inventory.SKU IS NULL
#standardSQL
# SKU の一つを使用して確認
SELECT * FROM `data-to-insights.ecommerce.products`
WHERE SKU = 'GGOEGATJ060517'
# クエリの結果、0 件のデータが返される
[実行] をクリックします。
商品在庫データセットで SKU が失われている可能性があるのはなぜですか。
解答: 簡単な答えはありません。ビジネス上の理由である可能性があります。
一部の SKU は在庫として保管しないデジタル商品の可能性がある
過去にウェブサイトからの注文で販売した古い商品が、現在の在庫に含まれていない
正当な理由で在庫から失われた、追跡が必要なデータである
商品在庫データセットには存在するものの、ウェブサイトから失われているデータはありますか。
別の結合のタイプを使用してクエリを作成し、調査します。
解答例:
#standardSQL
# 逆の結合を使用する
# ウェブサイトにはあって在庫にはないレコードを探す
SELECT DISTINCT
website.productSKU AS website_SKU,
inventory.SKU AS inventory_SKU
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
RIGHT JOIN `data-to-insights.ecommerce.products` AS inventory
ON website.productSKU = inventory.SKU
WHERE website.productSKU IS NULL
[実行] をクリックします。
解答: はい。2 つの商品 SKU がウェブサイトのデータセットから失われています。
次に、商品在庫データセットから他のフィールドを追加して詳細を確認します。
以下のクエリをコピーして貼り付けます。
#standardSQL
# どのような商品か?
# SELECT ステートメントにフィールドを追加する
SELECT DISTINCT
website.productSKU AS website_SKU,
inventory.*
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
RIGHT JOIN `data-to-insights.ecommerce.products` AS inventory
ON website.productSKU = inventory.SKU
WHERE website.productSKU IS NULL
#standardSQL
SELECT DISTINCT
website.productSKU AS website_SKU,
inventory.SKU AS inventory_SKU
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
FULL JOIN `data-to-insights.ecommerce.products` AS inventory
ON website.productSKU = inventory.SKU
WHERE website.productSKU IS NULL OR inventory.SKU IS NULL
SELECT DISTINCT
productSKU,
v2ProductCategory,
discount
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
CROSS JOIN ecommerce.site_wide_promotion
WHERE v2ProductCategory LIKE '%Clearance%'
[実行] をクリックします。
注: クロス結合の条件(例: ON や USING)は構文に含まれていません。フィールドは単純に最初のデータセットと乗算され、すべてのアイテムに 0.05 の割引が適用されます。
割引テーブルに予期せず複数のレコードが追加された場合の影響を確認しましょう。
以下のクエリをコピーして貼り付け、プロモーション テーブルにさらに 2 つのレコードを追加します。
#standardSQL
INSERT INTO ecommerce.site_wide_promotion (discount)
VALUES (.04),
(.03);
[実行] をクリックします。
次に、プロモーション テーブルのデータの値を確認します。
以下のクエリをコピーして貼り付けます。
#standardSQL
SELECT discount FROM ecommerce.site_wide_promotion
[実行] をクリックします。
82 件すべてのセール商品に割引を適用するとどうなりますか。
以下のクエリをコピーして貼り付けます。
#standardSQL
# 次のようになります。
SELECT DISTINCT
productSKU,
v2ProductCategory,
discount
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
CROSS JOIN ecommerce.site_wide_promotion
WHERE v2ProductCategory LIKE '%Clearance%'
#standardSQL
SELECT DISTINCT
productSKU,
v2ProductCategory,
discount
FROM `data-to-insights.ecommerce.all_sessions_raw` AS website
CROSS JOIN ecommerce.site_wide_promotion
WHERE v2ProductCategory LIKE '%Clearance%'
AND productSKU = 'GGOEGOLC013299'
#standardSQL
# 1 つの SKU に対して複数の product_names を表示した前述のクエリを再び呼び出す
SELECT
DISTINCT
COUNT(DISTINCT v2ProductName) AS product_count,
STRING_AGG(DISTINCT v2ProductName LIMIT 5) AS product_name,
productSKU
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE v2ProductName IS NOT NULL
GROUP BY productSKU
HAVING product_count > 1
ORDER BY product_count DESC
#standardSQL
# 1 つの SKU に関連付けられた 1 つの名前を取得する
WITH product_query AS (
SELECT
DISTINCT
v2ProductName,
productSKU
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE v2ProductName IS NOT NULL
)
SELECT k.* FROM (
# 商品を 1 つの配列に集約して
# 結果を 1 つだけ取得する
SELECT ARRAY_AGG(x LIMIT 1)[OFFSET(0)] k
FROM product_query x
GROUP BY productSKU # this is the field you want deduplicated
);