【发布时间】:2020-05-20 16:40:07
【问题描述】:
我正在尝试在单个表中的嵌套字段之间进行一些连接/聚合,并遇到 SQL 问题和“不支持引用其他表的相关子查询,除非它们可以去相关,例如通过转换它们变成了一个有效的 JOIN”错误。
我希望得到一些 SQL 帮助来解决一般问题,但我也很好奇如何处理该错误。
我的问题映射到BigQuery patents data。在该数据集中,专利具有分类数据(cpc 记录,其中cpc.code 是与相关数据cpc.inventive 和cpc.first 相关联的记录上的一个分类代码)。专利也有它引用的专利(citation 记录,其中citation.publication_number 是引用的专利,具有相关数据citation.type 和citation.category)。这些记录中有更多字段,但假设这些是重要的。
我想要得到的是类似这样的 json,每个 CPC 有一行,以及包含该 CPC 的专利如何根据被引用专利的 CPC 引用其他专利的信息的记录,以及关于 CPC 和引用的方面. json 看起来像这样:
[
{
"citing_patent_cpc": "1234/123",
"cited_patent_cpcs":
[
{
"cpc": "ABCD/345",
"citing_cpc_inventive": true,
"citing_cpc_first": false,
"citation_type": "ABC",
"citation_category": "A",
"cited_cpc_inventive": false,
"cited_cpc_first": true,
"count": 45
},
{
"cpc": "ABCD/345",
"citing_cpc_inventive": true,
"citing_cpc_first": false,
"citation_type": "ABC",
"citation_category": "A",
"cited_cpc_inventive": false,
"cited_cpc_first": false,
"count": 12
},
{
"cpc": "H211/123",
"citing_cpc_inventive": true,
"citing_cpc_first": false,
"citation_type": "ABC",
"citation_category": null,
"cited_cpc_inventive": true,
"cited_cpc_first": false,
"count": 3
},
...
]
},
{
"citing_patent_cpc": "1234/ABC",
"cited_patent_cpcs":
[
{
"cpc": "ABCD/345",
"citing_cpc_inventive": true,
"citing_cpc_first": false,
"citation_type": "ABC",
"citation_category": "A",
"cited_cpc_inventive": false,
"cited_cpc_first": true,
"count": 16
},
{
"cpc": "ABCD/345",
"citing_cpc_inventive": true,
"citing_cpc_first": false,
"citation_type": "ABC",
"citation_category": "A",
"cited_cpc_inventive": false,
"cited_cpc_first": false,
"count": 3
},
{
"cpc": "H211/123",
"citing_cpc_inventive": true,
"citing_cpc_first": false,
"citation_type": "ABC",
"citation_category": null,
"cited_cpc_inventive": true,
"cited_cpc_first": false,
"count": 9
},
...
]
},
...
]
每个唯一的cpc.code 获得一行和一个数组。该数组记录了专利引用的专利数量信息,其中行 CPC ("citing_patent_cpc") 具有特定的 CPC ("cpc"),其中包含两个专利的 CPC 和引用类型的各个方面。
例如,上例中的第一条记录意味着 45 次以 CPC“1234/123”作为发明 CPC 但不是第一个 CPC 的专利引用了另一项以 cpc“ABCD/345”作为第一个 CPC 的专利但没有创造性的 CPC,此引文属于“ABC”类型和“A”类。理论上,每一行都可以记录语料库中每个 CPC * 可能方面的数量,但实际上并非如此。
作为部分步骤,我尝试将 cpc 记录从被引专利加入到施引专利的记录中。我让这个查询直接在 SQL 中声明的一个非常小的表上工作,但它给出了“不支持引用其他表的相关子查询,除非它们可以去相关,例如通过将它们转换为有效的 JOIN。”当我尝试在大数据(如实际专利表)上运行它时。
查询如下:
SELECT
publication_number,
cpc,
citation,
(
SELECT ARRAY_CONCAT_AGG(cpc)
FROM `patents-public-data.patents.publications` AS JoinedPatents
RIGHT JOIN
(
SELECT publication_number
FROM UNNEST(Patents.citation)
) AS unnestedcitation
ON unnestedcitation.publication_number = JoinedPatents.publication_number) AS cited_cpc
FROM `patents-public-data.patents.publications`AS Patents
我很想知道:
- 如何使此查询正常工作而不会出现该错误。
- 我的问题的整体解决方案,如果有人对 SQL-fu 感到慷慨的话。
感谢所有阅读本文的人。
【问题讨论】:
标签: sql arrays join google-bigquery record