如何在BigQuery中加入两个表,但是JOIN参数已嵌套

发布于 2025-01-20 14:13:33 字数 399 浏览 1 评论 0原文

抱歉,新手问题刚刚开始学习SQL。我有两个表:

  1. 会话
  2. 项目

sessions表有问题(记录,重复)和内部问题item_id (字符串)

项目表具有主题(记录,重复)和内部主题prior_difficulty(string)。 项目表也有item_id(字符串)

我的目标是通过使用其item_id 将两个表加入,以获取会话及其prior_difficulty。 。 tia

sorry for the newbie questions, just started learning SQL. I have two tables:

  1. sessions
  2. items

sessions table has questions (RECORD, Repeated), and inside questions there's item_id (String)

items table has topics (RECORD, Repeated), and inside topics there's prior_difficulty (String). items table also has item_id (String)

My objective is to get a list of sessions and its prior_difficulty, by joining the two tables with their item_id. TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走就滚别墨迹 2025-01-27 14:13:33

您可以首先使用unnest()函数来从会话表中检索所有item_id s,然后从项目中与item_id一起加入它们表。
要从struct column 主题中检索prior_difficulty,您还可以使用unnest()函数:

select distinct
  sessions.session_id,
  t.prior_difficulty
from sessions, unnest(questions) q
left join items on q.item_id = items.item_id, unnest(topics) t

或者如果要创建重复记录列到组prior_difficultysession_id

select
  sessions.session_id,
  array_agg(distinct t.prior_difficulty ignore nulls) as prior_difficulties
from sessions, unnest(questions) q
left join items on q.item_id = items.item_id, unnest(topics) t
group by 1

You can first use the unnest() function to retrieve all the item_ids from the sessions table and then join them with the item_id from the items table.
To retrieve the prior_difficulty from your struct column topics, you can also use the unnest() function :

select distinct
  sessions.session_id,
  t.prior_difficulty
from sessions, unnest(questions) q
left join items on q.item_id = items.item_id, unnest(topics) t

or if you want to create a repeated record column to group prior_difficulty values by session_id :

select
  sessions.session_id,
  array_agg(distinct t.prior_difficulty ignore nulls) as prior_difficulties
from sessions, unnest(questions) q
left join items on q.item_id = items.item_id, unnest(topics) t
group by 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文