在查询之间传递结果并显示联合结果(google bigquery)

问题描述 投票:0回答:1

我想进行查询q1,并在第二个查询q1上使用q2的结果。我想显示q1q2的所有列,以便结果基于公共列。

((如果标题不清楚,请让我知道)

以下示例应在id中显示[publisherauthorq1]列。我想将它们传递给q2,为idcited_id列中的所有项目检索属性[categoryidq1]。

作为结果,对于每个id,我想显示所有quoted_id及其属性(包括id和quoted_id)。

或者,为更清楚起见,也可以为每个id检索一个quoted_id数组,在一个单独的查询中,我将用它们的属性修饰id和quoted_ids。

请同时就“性能”提出建议(我正在使用bigquery,因此,如果您能解释为什么一个解决方案更有效,可以帮助节省计算资源!)。

我想出了这个,但是不能显示q1的所有列。

WITH q1 AS (

    SELECT id, publisher, a.name
         FROM `db.publications`,
         UNNEST (publisher) as h,
         UNNEST (author) as a
         WHERE h Like '%penguin%'
)
SELECT p.id, c.id AS Cited, c.Category AS Cat 
     FROM `db.publications` AS p, UNNEST(citation) AS c 
     WHERE p.id IN (SELECT id from q1)

样本数据:

# result of q1
Row | Id | Publisher | Author
1   | item0 | penguin | Bob
2   | item0 | penguin | Alice
3   | item1 | penguin | Charlie

我想找到q1中每个唯一项(item0,item1)引用的其他项。

我希望以方便的格式获得结果,可以通过这种方式使用:

# Citations: books mentioned by item0, item1 ... 
item0 : [item10, item15, item100]
item1 : [item23, item0, item101, item15]
..

# Decorators : information about each book:
Row | Id | Publisher | Author(s) |  

我的问题是在单个查询中可以同时实现吗?如果是这样,将两个独立的查询拆分为较低的计算资源是否方便或更好?我的方法是先查询一组书籍及其装饰物,然后使用ID列表查找其引文。我不能随上面的示例一起携带装饰器。

sql performance google-bigquery with-statement
1个回答
0
投票

关于问题的第一部分,请使用联接引入where p.id in(select id from q1)字段,而不要使用q1。>

WITH q1 AS (

    SELECT id, publisher, a.name
         FROM `db.publications`,
         UNNEST (publisher) as h,
         UNNEST (author) as a
         WHERE h Like '%penguin%'
),
joined as (
  select id, p.citation, q1.publisher, q1.name
  from `db.publications` p
  inner join q1 using(id)
)
select id, c.id as Cited, c.Category as Cat
from joined
left join unnest(citation) c

© www.soinside.com 2019 - 2024. All rights reserved.