如何在BigQuery中使用offset和ordinal从包含不同数据类型的数组中选择一个元素?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在BigQuery中使用offset和ordinal从包含不同数据类型的数组中选择一个元素?相关的知识,希望对你有一定的参考价值。

基于这个页面accessing array elements,我创建了一个名为some_numbers的数组,它有两种数据类型(字符串和整数)。这是从数组中选择一个元素的查询,但它不起作用。

WITH sequences AS
  (SELECT array<struct<subject as string, marks as int64>>[("mat",87),("phy",45),("che",45),("zoo",78),("eng",87)] AS some_numbers
   UNION ALL SELECT array<struct<subject as string, marks as int64>>[("mat",87),("phy",45),("che",45),("zoo",78),("eng",87)] AS some_numbers
   UNION ALL SELECT array<struct<subject as string, marks as int64>>[("mat",87),("phy",45),("che",45),("zoo",78),("eng",87)] AS some_numbers)
SELECT some_numbers,
       some_numbers[OFFSET(1)] AS offset_1,
       some_numbers[ORDINAL(1)] AS ordinal_1
FROM sequences;

我有以下错误:

错误:语法错误:预期“,”或“>”但在[3:32]获得关键字AS -

答案

几乎相同的查询,但工作:

WITH sequences AS (
   SELECT           [STRUCT("mat" AS subject,87 AS marks),("phy",45),("che",45),("zoo",78),("eng",87)] AS some_numbers
   UNION ALL SELECT [STRUCT("mat" AS subject,87 AS marks),("phy",45),("che",45),("zoo",78),("eng",87)] AS some_numbers
   UNION ALL SELECT [STRUCT("mat" AS subject,87 AS marks),("phy",45),("che",45),("zoo",78),("eng",87)] AS some_numbers
)
SELECT some_numbers,
       some_numbers[OFFSET(1)] AS offset_1,
       some_numbers[ORDINAL(1)] AS ordinal_1
FROM sequences;

(对于将来的问题,请包括获得的错误和期望的结果)

以上是关于如何在BigQuery中使用offset和ordinal从包含不同数据类型的数组中选择一个元素?的主要内容,如果未能解决你的问题,请参考以下文章

对 Google Bigquery 中的嵌套字段使用 OFFSET 而不是 UNNEST

如何在 Google Bigquery 中提取括号内的字符? [SQL]

需要 SQL 查询帮助以解析 BigQuery 表中的 JSON 数据

BigQuery - 加载具有空值的 JSON 字段

如何在 forEach 循环中使用 offset().top

如何在 Entity Framework 5 中使用 SQL Server OFFSET 和 FETCH FIRST?