ここでは、ecommerce データセットを使用します。このデータセットには、Google Merchandise Store に関する数百万件の Google アナリティクス レコードが含まれており、BigQuery に読み込まれています。このデータセットのコピーを使用して、フィールドや行からどのような分析情報が得られるのかを確認します。
#standardSQL
# 探索対象の 1 日分の e コマースデータをコピー
CREATE OR REPLACE TABLE ecommerce.all_sessions_raw_20170801
OPTIONS(
description="Raw data from analyst team into our dataset for 08/01/2017"
) AS
SELECT fullVisitorId, * FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE date = '20170801' #56,989 件のレコード
;
Error: CREATE TABLE has columns with duplicate name fullVisitorId at [7:2]
#standardSQL
# 探索対象の 1 日分の e コマースデータをコピー
CREATE OR REPLACE TABLE ecommerce.all_sessions_raw_20170801
#スキーマ
(
fullVisitorId STRING OPTIONS(description="Unique visitor ID"),
channelGrouping STRING OPTIONS(description="Channel e.g. Direct, Organic, Referral...")
)
OPTIONS(
description="Raw data from analyst team into our dataset for 08/01/2017"
) AS
SELECT * FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE date = '20170801' #56,989 件のレコード
;
Error: The number of columns in the column definition list does not match the number of columns produced by the query at [5:1]
#standardSQL
# 探索対象の 1 日分の e コマースデータをコピー
CREATE OR REPLACE TABLE ecommerce.all_sessions_raw_20170801
#スキーマ
(
fullVisitorId STRING OPTIONS(description="Unique visitor ID"),
channelGrouping STRING OPTIONS(description="Channel e.g. Direct, Organic, Referral...")
)
OPTIONS(
description="Raw data from analyst team into our dataset for 08/01/2017"
) AS
SELECT fullVisitorId, city FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE date = '20170801' #56,989 件のレコード
;
Valid: This query will process 1.1 GiB when run.
ルール 2 を思い出してください。列リストと as query_statement 句の両方が存在する場合、BigQuery は、as query_statement 句内の名前を無視し、位置をもとにして列を列リストに一致させます。
[進行状況を確認] をクリックして、目標に沿って進んでいることを確認します。
テーブルを作成する
クエリ 4: ゲートキーパー
以下のクエリを BigQuery エディタで実行し、エラーを診断して次の質問に答えてください。
#standardSQL
# 探索対象の 1 日分の e コマースデータをコピー
CREATE OR REPLACE TABLE ecommerce.all_sessions_raw_20170801
#スキーマ
(
fullVisitorId STRING NOT NULL OPTIONS(description="Unique visitor ID"),
channelGrouping STRING NOT NULL OPTIONS(description="Channel e.g. Direct, Organic, Referral..."),
totalTransactionRevenue INT64 NOT NULL OPTIONS(description="Revenue * 10^6 for the transaction")
)
OPTIONS(
description="Raw data from analyst team into our dataset for 08/01/2017"
) AS
SELECT fullVisitorId, channelGrouping, totalTransactionRevenue FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE date = '20170801' #56,989 件のレコード
;
Valid: This query will process 907.52 MiB when run.
エラーを修正してクエリを再実行し、正常に実行されることを確認します。
クエリ 5: 正常に機能
以下のクエリを BigQuery エディタで実行し、次の質問に答えてください。
#standardSQL
# 探索対象の 1 日分の e コマースデータをコピー
CREATE OR REPLACE TABLE ecommerce.all_sessions_raw_20170801
#スキーマ
(
fullVisitorId STRING NOT NULL OPTIONS(description="Unique visitor ID"),
channelGrouping STRING NOT NULL OPTIONS(description="Channel e.g. Direct, Organic, Referral..."),
totalTransactionRevenue INT64 OPTIONS(description="Revenue * 10^6 for the transaction")
)
OPTIONS(
description="Raw data from analyst team into our dataset for 08/01/2017"
) AS
SELECT fullVisitorId, channelGrouping, totalTransactionRevenue FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE date = '20170801' #56,989 件のレコード
;
#standardSQL
# 探索対象の 1 日分の e コマースデータをコピー
CREATE OR REPLACE TABLE ecommerce.revenue_transactions_20170801
#スキーマ
(
fullVisitorId STRING NOT NULL OPTIONS(description="Unique visitor ID"),
visitId STRING NOT NULL OPTIONS(description="ID of the session, not unique across all users"),
channelGrouping STRING NOT NULL OPTIONS(description="Channel e.g. Direct, Organic, Referral..."),
totalTransactionRevenue FLOAT64 NOT NULL OPTIONS(description="Revenue for the transaction")
)
OPTIONS(
description="Revenue transactions for 08/01/2017"
) AS
SELECT DISTINCT
fullVisitorId,
CAST(visitId AS STRING) AS visitId,
channelGrouping,
totalTransactionRevenue / 1000000 AS totalTransactionRevenue
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE date = '20170801'
AND totalTransactionRevenue IS NOT NULL #XX 件のトランザクション
;
#standardSQL
SELECT DISTINCT
date,
fullVisitorId,
CAST(visitId AS STRING) AS visitId,
channelGrouping,
totalTransactionRevenue / 1000000 AS totalTransactionRevenue
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE totalTransactionRevenue IS NOT NULL
ORDER BY date DESC # 最新のトランザクション
LIMIT 100
;
#standardSQL
CREATE OR REPLACE VIEW ecommerce.vw_latest_transactions
AS
SELECT DISTINCT
date,
fullVisitorId,
CAST(visitId AS STRING) AS visitId,
channelGrouping,
totalTransactionRevenue / 1000000 AS totalTransactionRevenue
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE totalTransactionRevenue IS NOT NULL
ORDER BY date DESC # 最新のトランザクション
LIMIT 100
;
注: SELECT の対象がテーブルなのかビューなのかが、名前を見ただけではわかりにくいことがよくあります。そのため、ビュー名の先頭に vw_ を付けたり、末尾に _vw や _view を付けたりするのが慣例になっています。
OPTIONS を使用してビューに説明とラベルを付けることもできます。
以下のクエリをコピーして貼り付け、BigQuery で実行します。
#standardSQL
CREATE OR REPLACE VIEW ecommerce.vw_latest_transactions
OPTIONS(
description="latest 100 ecommerce transactions",
labels=[('report_type','operational')]
)
AS
SELECT DISTINCT
date,
fullVisitorId,
CAST(visitId AS STRING) AS visitId,
channelGrouping,
totalTransactionRevenue / 1000000 AS totalTransactionRevenue
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE totalTransactionRevenue IS NOT NULL
ORDER BY date DESC # 最新のトランザクション
LIMIT 100
;
#standardSQL
# 最新の 50 件のトランザクション
CREATE VIEW ecommerce.vw_latest_transactions # CREATE
OPTIONS(
description="latest 50 ecommerce transactions",
labels=[('report_type','operational')]
)
AS
SELECT DISTINCT
date,
fullVisitorId,
CAST(visitId AS STRING) AS visitId,
channelGrouping,
totalTransactionRevenue / 1000000 AS totalTransactionRevenue
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE totalTransactionRevenue IS NOT NULL
ORDER BY date DESC # 最新のトランザクション
LIMIT 50
;
答え: ビュー作成ステートメントが更新され、CREATE OR REPLACE ではなく単に CREATE となっているため、テーブルやビューがすでに存在する場合はそれらを上書きできません。第 3 の選択肢である CREATE VIEW IF NOT EXISTS を使用すると、テーブルやビューが存在しない場合にのみ作成され、存在する場合はエラーなしでスキップされます。
#standardSQL
CREATE OR REPLACE VIEW ecommerce.vw_large_transactions
OPTIONS(
description="large transactions for review",
labels=[('org_unit','loss_prevention')]
)
AS
SELECT DISTINCT
date,
fullVisitorId,
visitId,
channelGrouping,
totalTransactionRevenue / 1000000 AS revenue,
currencyCode
#v2ProductName
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE
(totalTransactionRevenue / 1000000) > 1000
AND currencyCode = 'USD'
ORDER BY date DESC # 最新のトランザクション
LIMIT 10
;
#standardSQL
CREATE OR REPLACE VIEW ecommerce.vw_large_transactions
OPTIONS(
description="large transactions for review",
labels=[('org_unit','loss_prevention')]
)
AS
SELECT DISTINCT
date,
fullVisitorId,
visitId,
channelGrouping,
totalTransactionRevenue / 1000000 AS totalTransactionRevenue,
currencyCode,
STRING_AGG(DISTINCT v2ProductName ORDER BY v2ProductName LIMIT 10) AS products_ordered
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE
(totalTransactionRevenue / 1000000) > 1000
AND currencyCode = 'USD'
GROUP BY 1,2,3,4,5,6
ORDER BY date DESC # 最新のトランザクション
LIMIT 10
ここでは 2 つの要素が追加されています。1 つは、各注文の商品を集計するための STRING_AGG() です。もう 1 つは、集計を実行するために必要な、その他のフィールドの GROUP BY です。
#standardSQL
SELECT DISTINCT
SESSION_USER() AS viewer_ldap,
REGEXP_EXTRACT(SESSION_USER(), r'@(.+)') AS domain,
date,
fullVisitorId,
visitId,
channelGrouping,
totalTransactionRevenue / 1000000 AS totalTransactionRevenue,
currencyCode,
STRING_AGG(DISTINCT v2ProductName ORDER BY v2ProductName LIMIT 10) AS products_ordered
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE
(totalTransactionRevenue / 1000000) > 1000
AND currencyCode = 'USD'
# ここにフィルタを追加
GROUP BY 1,2,3,4,5,6,7,8
ORDER BY date DESC # 最新のトランザクション
LIMIT 10
正解例:
#standardSQL
SELECT DISTINCT
SESSION_USER() AS viewer_ldap,
REGEXP_EXTRACT(SESSION_USER(), r'@(.+)') AS domain,
date,
fullVisitorId,
visitId,
channelGrouping,
totalTransactionRevenue / 1000000 AS totalTransactionRevenue,
currencyCode,
STRING_AGG(DISTINCT v2ProductName ORDER BY v2ProductName LIMIT 10) AS products_ordered
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE
(totalTransactionRevenue / 1000000) > 1000
AND currencyCode = 'USD'
AND REGEXP_EXTRACT(SESSION_USER(), r'@(.+)') IN ('qwiklabs.net')
GROUP BY 1,2,3,4,5,6,7,8
ORDER BY date DESC # 最新のトランザクション
LIMIT 10
上のクエリを実行して、返されたレコードが表示されることを確認します。
REGEXP_EXTRACT(SESSION_USER(), r'@(.+)') IN ('') の IN フィルタからすべてのドメインを削除してクエリをもう一度実行し、返されたレコードが 1 つも表示されないことを確認します。
#standardSQL
CREATE OR REPLACE VIEW ecommerce.vw_large_transactions
OPTIONS(
description="large transactions for review",
labels=[('org_unit','loss_prevention')],
expiration_timestamp=TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 90 DAY)
)
AS
#standardSQL
SELECT DISTINCT
SESSION_USER() AS viewer_ldap,
REGEXP_EXTRACT(SESSION_USER(), r'@(.+)') AS domain,
date,
fullVisitorId,
visitId,
channelGrouping,
totalTransactionRevenue / 1000000 AS totalTransactionRevenue,
currencyCode,
STRING_AGG(DISTINCT v2ProductName ORDER BY v2ProductName LIMIT 10) AS products_ordered
FROM `data-to-insights.ecommerce.all_sessions_raw`
WHERE
(totalTransactionRevenue / 1000000) > 1000
AND currencyCode = 'USD'
AND REGEXP_EXTRACT(SESSION_USER(), r'@(.+)') IN ('qwiklabs.net')
GROUP BY 1,2,3,4,5,6,7,8
ORDER BY date DESC # 最新のトランザクション
LIMIT 10;
注: expiration_timestamp オプションは永続テーブルにも適用できます。