如何获取数组的最后一个元素? SQL 大查询

How can I get the last element of an array? SQL Bigquery

我正在努力构建一个关注网络表单 Github 在 Google BigQuery 上的可用数据,例如:https://bigquery.cloud.google.com/table/githubarchive:day.20210606

关键数据包含在“payload”字段中,STRING类型。我设法取消嵌套该字段中包含的数据并将其转换为数组,但如何获取最后一个元素?

这是我目前所掌握的...

select type, 
  array(select trim(val) from unnest(split(trim(payload, '[]'))) val) payload
from `githubarchive.day.20210606` 
where type = 'MemberEvent'

输出:

如何只获取最后一个元素“Action”:“added”}? 我知道

select array_reverse(your_array)[offset(0)]

应该可以解决问题,但是我不确定如何将其组合到我的代码中。我一直在尝试不同的选项但没有成功,例如:

with  payload as ( select  array(select trim(val) from unnest(split(trim(payload, '[]'))) val) payload from `githubarchive.day.20210606`)

select type, ARRAY_REVERSE(payload)[ORDINAL(1)]

from `githubarchive.day.20210606` where type = 'MemberEvent'

所需的输出应如下所示:

要获取数组中的最后一个元素,您可以使用以下方法

select array_reverse(your_array)[offset(0)]   

I'm unsure how to combine that in my code

select  type, array_reverse(array(
    select trim(val) 
      from unnest(split(trim(payload, '[]'))) val
  ))[offset(0)] 
from `githubarchive.day.20210606`
where type = 'MemberEvent'