【问题标题】:Self-Joining across nested Records in BigQuery在 BigQuery 中跨嵌套记录的自联接
【发布时间】:2020-05-20 16:40:07
【问题描述】:

我正在尝试在单个表中的嵌套字段之间进行一些连接/聚合,并遇到 SQL 问题和“不支持引用其他表的相关子查询,除非它们可以去相关,例如通过转换它们变成了一个有效的 JOIN”错误。

我希望得到一些 SQL 帮助来解决一般问题,但我也很好奇如何处理该错误。

我的问题映射到BigQuery patents data。在该数据集中,专利具有分类数据(cpc 记录,其中cpc.code 是与相关数据cpc.inventivecpc.first 相关联的记录上的一个分类代码)。专利也有它引用的专利(citation 记录,其中citation.publication_number 是引用的专利,具有相关数据citation.typecitation.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

我很想知道:

  1. 如何使此查询正常工作而不会出现该错误。
  2. 我的问题的整体解决方案,如果有人对 SQL-fu 感到慷慨的话。

感谢所有阅读本文的人。

【问题讨论】:

    标签: sql arrays join google-bigquery record


    【解决方案1】:

    我认为这应该是您想要的查询的近似值。它看起来是一个大数据集,所以我无法评论速度/效率。希望逻辑至少是有道理的。

    with data as (
    -- unnest your data
      select 
        p.publication_number,
        cp.code as cpc_code,
        cp.inventive as cpc_inventive,
        cp.first as cpc_first,
        ci.publication_number as citation_publication_number,
        ci.type as citation_type,
        ci.category as citation_category
      from `patents-public-data.patents.publications` p
      left join unnest(cpc) cp
      left join unnest(citation) ci
    ),
    joined as (
    -- do a self-join to join citation publication_number to original publication_number, group to get counts
      select 
        d1.cpc_code as citing_patent_cpc,
        d2.cpc_code as cpc,
        d1.cpc_inventive as citing_cpc_inventive,
        d1.cpc_first as citing_cpc_first,
        d1.citation_type,
        d1.citation_category,
        d2.cpc_inventive as cited_cpc_inventive,
        d2.cpc_first as cited_cpc_first,
        count(*) as count
      from data d1
      left join data d2 on d1.citation_publication_number = d2.publication_number
      group by 1,2,3,4,5,6,7,8
    ),
    agged as (
    -- aggrecate to match requested output
      select 
        citing_patent_cpc,
        array_agg(struct(cpc,citing_cpc_inventive,citing_cpc_first,citation_type,citation_category,cited_cpc_inventive,cited_cpc_first,count)) cited_patent_cpcs
      from joined
      group by 1
    )
    select * from agged
    

    【讨论】:

    • 谢谢!这行得通。希望有任何帮助将第一个查询变成没有相关子查询错误的东西。
    • 无法为您提供帮助。自从切换到 BQ 后,我几乎只使用 CTE 处理所有事情。
    猜你喜欢
    • 1970-01-01
    • 2021-02-02
    • 2021-12-04
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    相关资源
    最近更新 更多