BigQuery分组依据中的第N个结果

时间:2019-07-14 15:54:46

标签: google-bigquery

我有一个派生表,例如:

id, desc, total, account
1, one, 10, a
1, one, 9, b
1, one, 3, c
2, two, 27, c

我可以做一个简单的

select id, desc, sum(total) as total from mytable group by id

但是我想将等效的first(account),first(total),second(account),second(total)添加到输出中,所以它应该是:

id, desc, total, first_account, first_account_total, second_account, second_account_total    
1, one, 21, a, 10, b, 9
2, two, 27, c, 27, null, 0

有指针吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

以下是用于BigQuery标准SQL

#standardSQL
SELECT id, `desc`, total,
  arr[OFFSET(0)].account AS first_account, 
  arr[OFFSET(0)].total AS first_account_total,
  arr[SAFE_OFFSET(1)].account AS second_account, 
  arr[SAFE_OFFSET(1)].total AS second_account_total
FROM (
  SELECT id, `desc`, SUM(total) total, 
    ARRAY_AGG(STRUCT(account, total) ORDER BY total DESC LIMIT 2) arr
  FROM `project.dataset.table`
  GROUP BY id, `desc`
)

如果需要两个以上的第一个容器,我将使用以下模式,该模式可以避免重复使用arr[SAFE_OFFSET(1)].total AS second_account_total之类的粗重复行

#standardSQL
SELECT * FROM (SELECT NULL id, '' `desc`, NULL total, '' first_account, NULL first_account_total, '' second_account, NULL second_account_total) WHERE FALSE 
UNION ALL
SELECT id, `desc`, total, arr[OFFSET(0)].*, arr[SAFE_OFFSET(1)].* 
FROM (
  SELECT id, `desc`, SUM(total) total, 
    ARRAY_AGG(STRUCT(account, total) ORDER BY total DESC LIMIT 2) arr
  FROM `project.dataset.table`
  GROUP BY id, `desc`
)

在上面,第一行设置输出的布局,同时由于WHERE FALSE而根本不返回任何行,因此,我不需要显式地解析struct的元素并提供别名