BigQuery Export 查询示例

本页面提供了有关如何查询导出到 BigQuery 数据集的渠道服务数据的示例。

如需详细了解渠道服务数据导出,请参阅如何设置将渠道服务数据导出至 BigQuery。如需查看其他问题排查提示,请参阅排查 BigQuery Export 问题

识别和访问 BigQuery 表

渠道服务导出是使用 Partner Sales Console 设置的。

表的名称使用以下格式:PROJECT_ID.DATASET_NAME.reseller_billing_detailed_export_v1

如需允许其他用户查看渠道服务导出内容,您需要应用正确的权限。例如,您可以将 bigquery.dataViewer 角色分配给项目、数据集或表本身的凭据。

查询示例

以下示例包含一个示例查询以及有关如何解读结果的基本说明。结果包含 Partner Sales Console 中已配置的每个客户或渠道合作伙伴的 Google Cloud 使用费。

合作伙伴费用总和(按结算账号)

您可以使用此查询来验证从 Google 收到的账单。值 costcredit.amount 是所有行的总和。其中包括使用费用、税费、调整项和舍入误差。

SELECT
  payer_billing_account_id,
  currency,
  invoice.month,
  SUM(cost)
    + SUM(
      IFNULL(
        (SELECT SUM(c.amount) FROM UNNEST(credits) AS c), 0))
    AS total
FROM `PROJECT_ID.DATASET_NAME.reseller_billing_detailed_export_v1`
WHERE
  export_time BETWEEN TIMESTAMP(START_DATE)
    AND TIMESTAMP(END_DATE)
GROUP BY
  payer_billing_account_id,
  currency,
  invoice.month
ORDER BY
  payer_billing_account_id,
  currency,
  invoice.month;

客户费用总和(按 Cloud Billing 子帐号)

您可以使用此查询汇总每个客户的可计费用量。值 customer_costcredit.customer_amount 显示所有代表使用费用的行的总和。表示税费、调整项和舍入的行会显示 null 值。

对于 type 设置为 RESELLER_MARGIN 的赠金,其 customer_amount 会设置为零,以确保在客户结算中不显示利润率。

customer_costcredit.customer_amount 表示您的重新定价配置。例如,如果特定客户的 Google Cloud 使用权的 RebillingBasis 设置为 Direct Customer Cost minus 5%,则 customer_cost 值为 0.95 乘以 cost。这反映了配置的费用减少额。

此查询的结果包含每个 Cloud Billing 子帐号、其关联的合作伙伴销售客户资源名称、账单月份和可结算总用量。

SELECT
  customer_name,
  billing_account_id,
  payer_billing_account_id,
  currency,
  invoice.month,
  SUM(customer_cost)
    + SUM(
      IFNULL(
        (SELECT SUM(c.customer_amount) FROM UNNEST(credits) AS c), 0))
    AS total
FROM `PROJECT_ID.DATASET_NAME.reseller_billing_detailed_export_v1`
WHERE
  export_time BETWEEN TIMESTAMP(START_DATE)
    AND TIMESTAMP(END_DATE)
GROUP BY
  customer_name,
  billing_account_id,
  payer_billing_account_id,
  currency,
  invoice.month
ORDER BY
  customer_name,
  billing_account_id,
  payer_billing_account_id,
  currency,
  invoice.month;

按 Cloud Billing 子帐号划分的客户费用总和(向子帐号所有者显示)

此查询展示了数据费用数字与转销商的客户(或作为合作伙伴的您)在打开其子帐号的 Google Cloud 控制台时看到的数据对应关系。

  1. 对于 type 设置为 RESELLER_MARGIN 的赠金,SBA 视图中将隐藏此值。
  2. 对于 cost_type税费的费用,此字段在 SBA 视图中处于隐藏状态。转销商应单独将适用税费添加到其转销商的账单中。

有时,某些记录的 cost_at_list 可能为 null,导致此结果与您在 Google Cloud 控制台中看到的内容不同。如果发生这种情况,请改用 IFNULL(cost_at_list, cost)。如需了解详情,请参阅无列表费用的费用

SELECT
  customer_name,
  currency,
  SUM(cost_at_list) AS list_cost,
  SUM(cost - cost_at_list) AS negotiated_savings,
  SUM(cost)
    + SUM(
      IFNULL(
        (
          SELECT
            SUM(c.amount)
          FROM
            UNNEST(credits) AS c
          WHERE
            c.type != 'RESELLER_MARGIN'
        ),
        0)) AS total
FROM
  `PROJECT_ID.DATASET_NAME.reseller_billing_detailed_export_v1`
WHERE
  cost_type != 'tax'
  AND billing_account_id = 'BILLING_SUBACCOUNT_ID'
  AND invoice.month = 'YYYYMM'
GROUP BY
  customer_name,
  currency
ORDER BY
  customer_name,
  currency;

渠道合作伙伴费用总和(按结算账号)

如果您是分销商,则可以使用此查询汇总每个渠道合作伙伴的可计费用量。值 channel_partner_costcredit.channel_partner_amount 表示代表使用费的所有行的总和。表示税费、调整项和四舍五入的行会显示 null 值。

对于 type 设置为 RESELLER_MARGIN 的赠金,其 channel_partner_amount 会设置为零,以确保在渠道合作伙伴结算中不显示利润。

channel_partner_costcredit.channel_partner_amount 表示您的重新定价配置。例如,如果特定渠道合作伙伴的 RebillingBasis 设为 Direct Customer Cost minus 5%,则 channel_partner_cost 值为 0.95 乘以 cost。这反映了配置费用降幅。

此查询的结果包含每个结算帐号、在该结算帐号下与客户关联的渠道合作伙伴 resource_name、账单月份和可结算总用量。

SELECT
  channel_partner_name,
  payer_billing_account_id,
  currency,
  invoice.month,
  SUM(channel_partner_cost)
    + SUM(
      IFNULL(
        (SELECT SUM(c.channel_partner_amount) FROM UNNEST(credits) AS c), 0))
    AS total
FROM `PROJECT_ID.DATASET_NAME.reseller_billing_detailed_export_v1`
WHERE
  export_time BETWEEN TIMESTAMP(START_DATE)
    AND TIMESTAMP(END_DATE)
GROUP BY
  channel_partner_name,
  payer_billing_account_id,
  currency,
  invoice.month
ORDER BY
  channel_partner_name,
  payer_billing_account_id,
  currency,
  invoice.month;

未分配给客户的费用总和

您可以使用此查询汇总未与渠道服务客户相关联的费用,但可能表示对其他客户计费的用量。值 costcredit.amount 显示 billing_account_idpayer_billing_account_id 不相同的所有行的总和。billing_account_id 代表 Cloud Billing 子帐号。

对于 type 设置为 RESELLER_MARGIN 的赠金,此查询不包括赠金金额。赠金金额仅供您分配,不会被视为客户费用。RESELLER_MARGIN 金额包含在您的费用中,并反映在 Google 发送的账单中。

SELECT
  billing_account_id,
  payer_billing_account_id,
  currency,
  invoice.month,
  SUM(cost)
    + SUM(
      IFNULL(
        (SELECT SUM(c.amount) FROM UNNEST(credits) AS c WHERE c.type != 'RESELLER_MARGIN'), 0))
    AS total
FROM `PROJECT_ID.DATASET_NAME.reseller_billing_detailed_export_v1`
WHERE
  export_time BETWEEN TIMESTAMP(START_DATE)
    AND TIMESTAMP(END_DATE)
  AND customer_name IS NULL
GROUP BY
  billing_account_id,
  payer_billing_account_id,
  currency,
  invoice.month
ORDER BY
  billing_account_id,
  payer_billing_account_id,
  currency,
  invoice.month;

Google Workspace 数据视图与 CSV 账单类似

如果您是 Google Workspace 转销商,则可能会看到 Google Workspace 结算数据的视图,其中包含与账单 CSV 文件类似的列。

SELECT
  (
    SELECT
      ws_labels.value
    FROM
      UNNEST(system_labels) AS ws_labels
    WHERE
      ws_labels.key = 'workspace.googleapis.com/domain_name'
  ) AS domain_name,
  billing_account_id AS customer_id,
  sku.description AS sku_name,
  sku.id AS sku_id,
  (
    SELECT
      ws_labels.value
    FROM
      UNNEST(system_labels) AS ws_labels
    WHERE
      ws_labels.key = 'workspace.googleapis.com/usage_type'
  ) AS description,
  (
    SELECT
      ws_labels.value
    FROM
      UNNEST(system_labels) AS ws_labels
    WHERE
      ws_labels.key = 'workspace.googleapis.com/order_id'
  ) AS order_name,
  FORMAT_TIMESTAMP('%b %d ', usage_start_time, 'America/Los_Angeles')
    AS start_date,
  FORMAT_TIMESTAMP(
    '%b %d',
    TIMESTAMP_SUB(usage_end_time, INTERVAL 1 MINUTE),
    'America/Los_Angeles')
    AS end_date,
  SUM(usage.amount_in_pricing_unit) AS quantity,
  (
    SELECT ws_labels.value
    FROM UNNEST(system_labels) AS ws_labels
    WHERE ws_labels.key = 'workspace.googleapis.com/purchase_order_id'
  ) AS po_number,
  SUM(cost) AS amount,
FROM `PROJECT_ID.DATASET_NAME.reseller_billing_detailed_export_v1`
WHERE
  export_time BETWEEN TIMESTAMP(START_DATE)
    AND TIMESTAMP(END_DATE)
  AND payer_billing_account_id = 'EXTERNAL_BILLING_ACCOUNT_ID'
  AND invoice.month = 'INVOICE_MONTH'
GROUP BY
  domain_name,
  customer_id,
  sku_name,
  sku_id,
  description,
  order_name,
  start_date,
  end_date,
  po_number
HAVING amount != 0
ORDER BY
  domain_name,
  order_name,
  start_date,
  end_date;

要添加到 SELECTGROUP BY 子句的其他相关列:

  • billing_account_id:表示客户的 Cloud Identity ID。
  • customer_name:表示可在 API 调用中使用的客户资源。
  • channel_partner_name:(适用于分销商)代表渠道合作伙伴。
  • entitlement_name:渠道服务中使用权的资源名称。
  • customer_correlation_id:您可以为客户定义的客户关系管理 (CRM) ID。
  • usage.amount:对于基于 Google Workspace 席位的使用情况,这表示付费席位。例如,为承诺购买的席位或为灵活订阅分配的席位数。

查询结果与 CSV 账单在以下几个方面有所不同:

  • CSV“说明”列使用人类可读的字符串,而 BigQuery Export 使用枚举值。
  • CSV“开始/结束日期”列没有填充零(例如 5 月 1 日),而 BigQuery 查询使用的是填充的值(例如 5 月 1 日)。您也可以直接使用时间戳值。usage_start_time 含边界值,但 usage_end_time 不含边界值。
  • CSV 文件的末尾只有一个“税费”行,而 BigQuery Export 在订阅级别包含税费行,其金额总和与 CSV 中的“税费”行相同。