Get the top patent countries, codes in a BQ public dataset

后端 未结 1 1592
自闭症患者
自闭症患者 2021-01-26 10:38

I am trying to use an analytic function to get the top 2 countries with patent applications, and within those top 2 countries, get the top 2 application kinds. For example, the

1条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-26 11:10

    Below is for BigQuery Standard SQL

    #standardSQL
    WITH A AS (
      SELECT country_code
      FROM `patents-public-data.patents.publications`
      GROUP BY country_code
      ORDER BY COUNT(1) DESC
      LIMIT 2
    ), B AS (
      SELECT
        country_code,
        application_kind,
        COUNT(1) application_kind_count
      FROM `patents-public-data.patents.publications`
      WHERE country_code IN (SELECT country_code FROM A)
      GROUP BY country_code, application_kind
    ), C AS (
      SELECT
        country_code,
        application_kind,
        application_kind_count,
        DENSE_RANK() OVER(PARTITION BY country_code ORDER BY application_kind_count DESC) AS application_kind_rank
      FROM B
    )
    SELECT
      country_code,
      application_kind,
      application_kind_count
    FROM C
    WHERE application_kind_rank <= 2  
    

    with result

    0 讨论(0)
提交回复
热议问题