SQL - 有选择地将项目放入数组

SQL - selectively put items into array

有没有一种方法可以在 BQ 中使用 ARRAY_AGG 有选择地选择要放入数组中的内容?我有以下 table,我试图将延迟案例放入嵌套在相应最终案例下的数组中。

case_id  status    date       primary_date primary_case_id primary_status
abc123   deferred  2022-05-01 2022-08-01   ghi789          final
def456   deferred  2022-07-01 2022-08-01   ghi789          final
ghi789   final     2022-08-01 2022-08-01   ghi789          final
jkl123   deferred  2022-10-01 2022-12-01   mno456          final
mno456   final     2022-12-01 2022-12-01   mno456          final

但是我在使用 ARRAY_AGG 时得到类似的结果,其中最后一个案例也包含在数组中。

primary_date primary_case_id primary_status deferred_cases 
2022-08-01   ghi789          final          case_id  status    date
                                            ghi789   final     2022-08-01
                                            def456   deferred  2022-07-01
                                            abc123   deferred  2022-05-01
2022-12-01   mno456          final          case_id  status    date
                                            mno456   final     2022-12-01
                                            jkl123   deferred  2022-10-01

有没有办法从数组中删除最后的 cases 以获得下面的结果?

primary_date primary_case_id primary_status deferred_cases 
2022-08-01   ghi789          final          case_id  status    date
                                            def456   deferred  2022-07-01
                                            abc123   deferred  2022-05-01
2022-12-01   mno456          final          case_id  status    date
                                            jkl123   deferred  2022-10-01

考虑以下方法

select * replace(
  array(select as struct * 
    from t.deferred_cases
    where date != primary_date
    order by date desc
  ) as deferred_cases)
from (
  select primary_case_id,
    array_agg(struct(primary_date, primary_status) order by date desc limit 1)[offset(0)].*, 
    array_agg(struct(case_id, status, date)) deferred_cases
  from your_table
  group by primary_case_id
) t