我只是想解决一些简单的例子来提高我在BigQuery中编写查询的技巧。在下面的例子中, 我不知道如何删除空行。
with table1 as(
select "box_1" box, "yellow" colours union all
select "box_1" box, "green" colours union all
select "box_2" box, "blue" colours union all
select "box_2" box, "blue" colours union all
select "box_3" box, "red" colours union all
select "box_3" box, "green" colours union all
select "box_3" box, "blue" colours
)
select array(select box from unnest(x)y where "blue" in unnest(x) limit 1 )box_containing_blue
from(select box,array_agg(if(colours="blue",colours,null)ignore nulls )x
from table1 group by box)
答案 0 :(得分:1)
我相信这样的事情可以解决问题
with table1 as(
select "box_1" box, "yellow" colours union all
select "box_1" box, "green" colours union all
select "box_2" box, "blue" colours union all
select "box_2" box, "blue" colours union all
select "box_3" box, "red" colours union all
select "box_3" box, "green" colours union all
select "box_3" box, "blue" colours
)
select array(select box from unnest(x)y where "blue" in unnest(x) limit 1 )as box_containing_blue
from(select box,array_agg(if(colours="blue",colours,null)ignore nulls )x
from table1 group by box
having x is not null)
答案 1 :(得分:0)
我不确定您真正尝试使用该查询实现的目标。 但如果这是我所相信的,我认为你可以像这样简化:
with table1 as(
select "box_1" box, "yellow" colours union all
select "box_1" box, "green" colours union all
select "box_2" box, "blue" colours union all
select "box_2" box, "blue" colours union all
select "box_3" box, "red" colours union all
select "box_3" box, "green" colours union all
select "box_3" box, "blue" colours
)
select distinct box as box_containing_blue from table1 where colours = "blue"