我有一个像这样的表:
col1 col2 num
a <string1> 5
a <string2> 10
a <string3> 0
a <string4> 7
b <string1> 6
b <string2> 3
b <string3> 20
b <string4> 1
我想为col1(或最终col2)的每个值选择最小值,因此所需的输出为:
col1 col2 num
a <string3> 0
b <string4> 1
我该如何实现?我正试图在BigQuery中做到这一点。
答案 0 :(得分:1)
以下是用于BigQuery标准SQL
#standardSQL
select as value array_agg(t order by num limit 1)[offset(0)]
from `project.dataset.table` t
group by col1
是否适用于您的问题的示例数据,如以下示例所示
#standardSQL
WITH `project.dataset.table` AS (
select 'a' col1, '<string1>' col2, 5 num union all
select 'a', '<string2>', 10 union all
select 'a', '<string3>', 0 union all
select 'a', '<string4>', 7 union all
select 'b', '<string1>', 6 union all
select 'b', '<string2>', 3 union all
select 'b', '<string3>', 20 union all
select 'b', '<string4>', 1
)
select as value array_agg(t order by num limit 1)[offset(0)]
from `project.dataset.table` t
group by col1
输出为
答案 1 :(得分:0)
如果您正在运行Postgres(带有标签),则可以使用expenses.csv
:
http
在BigQuery中(如问题所述),您可以使用数组来做到这一点:
distinct on