1

我希望将以下旧版 SQL 查询转换为标准 SQL。我遇到的问题是我需要取消嵌套两个表(标签和学分)。如何转换此查询?谢谢!

每当我尝试重写此查询时,都会遇到“标量子查询产生了多个元素”(见下文)。

我正在尝试重写的旧版 SQL 查询:

SELECT
  service.description,
  sku.description,
  usage_start_time,
  usage_end_time,
  labels.key,
  labels.value,
  cost,
  usage.amount,
  project.name,
  credits.name,
  credits.amount
FROM
  flatten([gcp_billing_data.gcp_billing_export],
    credits)

到目前为止我在标准 SQL 中尝试过的内容:

SELECT
  service.description AS service,
  sku.description AS sku,
  usage_start_time,
  usage_end_time,
  l.key,
  l.value,
  cost,
  usage.amount AS usage,
  project.name AS project,
  c.name AS credit,
  c.amount
FROM
  `gcp_billing_data.gcp_billing_export`,
  UNNEST(labels) AS l,
  UNNEST(credits) AS c
Group by 1,2,3,4,5,7,8,9,10,11

此查询运行,但行数明显少于我的预期。

4

1 回答 1

1

在标准 SQL 中对您的查询进行快速而正式的修复就像在下面替换

(select l.value from unnest(labels) as l)    

(select string_agg(l.value) from unnest(labels) as l)   

但它仍然与您的 Legacy SQL 查询版本的初始版本不完全相同

于 2019-04-23T22:58:41.113 回答