BigQuery:动态将行转换为列

huangapple go评论60阅读模式
英文:

BigQuery : transform row into columns dynamically

问题

BigQuery 中,

表 issue_fields:

issue_id field_name field_value
101 city NY
101 size 10 m2
103 postalcode 1020
103 city SF

表 issues:

| issue_id | created_date |

我想在 BigQuery 中创建一个表,该表是 issues 表,具有 n 个新列,每列都是一个 field_name,对于 issue_fields 表中的所有 field_name。

期望输出:

issue_id created_date city size postalcode 
101 2023 NY 10 m2
103 2022 SF 1020
英文:

In BigQuery

Table issue_fields :

issue_id field_name field_value
101 city NY
101 size 10 m2
103 postalcode 1020
103 city SF

Table issues :

| issue_id | created_date

I want, in BigQuery, to create a table which is the issues table with n new columns, every column is a field_name, for all field_name in the table issue_fields.

Desired output :

issue_id created_date city size postalcode 
101 2023 NY 10 m2
103 2022 SF 1020

Tried with ChatGPT without success

答案1

得分: 1

I'm sorry, but I can't provide a translation for code as it falls under a specialized technical domain. If you have any other non-code related content you'd like translated, feel free to ask!

英文:

you need a script to accomplish this

    BEGIN
    DECLARE dynamic_fields STRING ;
    -- This execute block selects all field_name
    EXECUTE IMMEDIATE FORMAT("""
      WITH
        fields AS (
          SELECT 101 AS issue_id, "city" AS field_name, "NY" as field_value
          UNION ALL
          SELECT 101 AS issue_id, "size" AS field_name, "10m2" as field_value
          UNION ALL
          SELECT 103 AS issue_id, "postalcode" AS field_name, "1020" as field_value
          UNION ALL
          SELECT 103 AS issue_id, "county" AS field_name, "SF" as field_value
      ) SELECT 

---- The string_agg the list of all possible values you want to select ----

string_agg( DISTINCT CONCAT("'",fields.field_name,"'") )
      FROM fields
    """) INTO dynamic_fields;
    SELECT dynamic_fields;
    
    EXECUTE IMMEDIATE FORMAT("""
         WITH
         fields_set AS (
           SELECT 101 AS issue_id, "city" AS field_name, "NY" as field_value
           UNION ALL
           SELECT 101 AS issue_id, "size" AS field_name, "10m2" as field_value
           UNION ALL
           SELECT 103 AS issue_id, "postalcode" AS field_name, "1020" as field_value
           UNION ALL
           SELECT 103 AS issue_id, "county" AS field_name, "SF" as field_value
       ) SELECT *
       FROM fields_set

-- This pivot section selects into the distinct list in previous block

       PIVOT(COUNT(fields_set.issue_id) FOR fields_set.field_name IN (%s))""",dynamic_fields);
    END;

答案2

得分: 0

你可以使用条件聚合:

select issue_id,
       max(created_date) as created_date,
       max(case when field_name = 'city' then field_value end ) as city,
       max(case when field_name = 'size' then field_value end ) as size,
       max(case when field_name = 'postalcode' then field_value end ) as postalcode
from mytable
group by issue_id
英文:

You can use the conditional aggregation :

select issue_id,
       max(created_date) as created_date,
       max(case when field_name = 'city' then field_value end ) as city,
       max(case when field_name = 'size' then field_value end ) as size,
       max(case when field_name = 'postalcode' then field_value end ) as postalcode
from mytable
group by issue_id

答案3

得分: -1

以下是要翻译的内容:

使用以下代码,您可以获取以下输出(不包括`created_date`字段)。

SELECT
  issue_id,
  MAX(IF(field_name = 'city', field_value, NULL)) AS city,
  MAX(IF(field_name = 'size', field_value, NULL)) AS size,
  MAX(IF(field_name = 'postalcode', field_value, NULL)) AS postalcode
FROM
  your_table
GROUP BY
  issue_id

这将使用if语句提取键值对,返回所需的输出表。

英文:

You can get the following output (excluding the created_date field) using the below code.

SELECT
  issue_id,
  MAX(IF(field_name = 'city', field_value, NULL)) AS city,
  MAX(IF(field_name = 'size', field_value, NULL)) AS size,
  MAX(IF(field_name = 'postalcode', field_value, NULL)) AS postalcode
FROM
  your_table
GROUP BY
  issue_id

This will use if statements to extract the key value pairs, returning the desired output table.

huangapple
  • 本文由 发表于 2023年6月27日 17:27:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定