一对多加入表但复制记录 bigquery

Join the tables one to many but duplicate the records bigquery

我正在研究大查询。 我有两个 table:一个用于站点,一个用于站点日志。 我想查询选择所有站点(不重复它们)并按日期告诉我最后状态。 我还想知道第 1 组或第 2 组对每个站点给出的答案。 我确实 left join 并复制了所有内容,我已经尝试过进行子查询,但它给了我一个错误。我该如何解决?

Table 个站点

ID  name    link
1   ff     sa.com
2   rg     knakans.com
3   g      aklsfndkl.com
4   gg     a.com
5   g      g.com

table 站点日志

ID  IDsite  Status  timestamp   responde   submittedBy
1   2       backlog     20/2    denied      team1
2   1       pending     20/3    null        team2
3   3       pending     20/4    null        team2
4   3       approved    20/5    ok          team1
5   3       submitted   20/6    ok          team2

我的查询

select
id, 
name, 
link, 
  FIRST_VALUE(b.status) OVER(PARTITION BY b.IDsite ORDER BY b.timestamp DESC) as lastStatus,
case when (submittedBy='team1')  then response else "" end  as  team1_response,
case when (submittedBy='team2')  then response else "" end  as  team2_response,

from sites a 
left join siteLogs b
where b.IDsite=a.ID

我希望得到什么

ID  name    link           lastStatus   team1_response  team2_response
1   ff      sa.com         pending      null             ok
2   rg      knakans.com    backlog      denied           null
3   g       aklsfndkl.com  approved     ok               ok
4   gg      a.com          null         null             null
5   g       g.com          null         null             null 

试试下面

select a.id, name, link, 
  array_agg(status order by timestamp desc limit 1)[offset(0)] lastStatus,
  array_agg(if(submittedBy = 'team1', response, null) order by if(submittedBy = 'team1', timestamp, null) desc limit 1)[offset(0)] team1_response,
  array_agg(if(submittedBy = 'team2', response, null) order by if(submittedBy = 'team2', timestamp, null) desc limit 1)[offset(0)] team2_response
from sites a
left join siteLogs b
on a.id = b.idsite
group by a.id, name, link           

如果应用于您问题中的示例数据 - 输出为