BigQuery Export 查询示例

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

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

识别和访问 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,
  SUM(cost_at_list) AS list_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 的“Description”(说明)列使用直观易懂的字符串,而 BigQuery 导出内容使用枚举值。
  • CSV 中的“开始/结束日期”列未填充零(例如 5 月 1 日),而 BigQuery 查询使用填充值(例如 5 月 01 日)。您也可以直接使用时间戳值。usage_start_time 包含在范围内,usage_end_time 不包含在范围内。
  • CSV 在末尾只有一行“税费”,而 BigQuery 导出文件在订阅级别有税费行,其总金额与 CSV 的“税费”行相同。

与账单 PDF 文件类似的线下订单结算数据视图(预览版)

如果您是转销商,并且还下达线下订单(即未使用 Partner Sales Console 下达的订单),则可以查看结算数据视图,其中包含与账单 CSV 文件类似的列。

SELECT
  billing_account_id AS customer_name,
  service.description AS sku_name,
  sku.id AS sku_id,
  FORMAT_TIMESTAMP('%b %d', usage_start_time, 'America/Los_Angeles')
    AS start_date,
  FORMAT_TIMESTAMP('%b %d', usage_end_time, 'America/Los_Angeles')
    AS end_date,
  SUM(usage.amount_in_pricing_unit) AS quantity,
  SUM(cost) AS amount,
FROM `PROJECT_ID.DATASET_NAME.reseller_billing_offline_orders_detailed_export_v0`
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
  customer_name,
  sku_name,
  sku_id,
  start_date,
  end_date
HAVING amount != 0
ORDER BY
  customer_name,
  start_date,
  end_date,
  sku_id;

查询结果与 PDF 账单的不同之处体现在以下几个方面:

  • 您可以将时间戳值转换为“America/Los_Angeles”时区中的日期值。usage_start_timeusage_end_time 都是排他的。
  • PDF 中详细列出了本地销售税和州销售税,而 BigQuery 导出内容包含订阅级别的税费明细,这些明细的总和为整个订阅的税费金额。