【问题标题】:How to convert flat/relational table to a key/value pair table in BigQuery?如何将平面/关系表转换为 BigQuery 中的键/值对表?
【发布时间】:2017-02-22 11:45:46
【问题描述】:

我有一个表,其中多行具有相同的数据,除了一列在所有重复行中具有唯一值。

例子:

userid, article_count, test_count, total_articles,total_tests,exam_id, exam_score

- 00016320-452b-11e6-9a4a-252aad95e99b,38,1,106,88,e9c196a1-4ae6-11e5-bc68-8620ffdeb79c,1
- 00016320-452b-11e6-9a4a-252aad95e99b,38,1,106,88,8223ff18-d538-11e5-80ff-b0086ec8f4cd,1
- 00016320-452b-11e6-9a4a-252aad95e99b,38,1,106,88,be2ac525-3909-11e6-a224-56a308185daf,1

我想在表中为每个用户保留一行,并将exam_id、exam_score 视为键/值对或BigQuery 中的记录。 我将考试视为具有两个子字段的记录:exam.exam_id 和exam.score。

输出如下:

userid, article_count, test_count, total_articles,total_tests,exam.exam_id, exam.score

- 00016320-452b-11e6-9a4a-252aad95e99b,38,1,106,88,e9c196a1-4ae6-11e5-bc68-8620ffdeb79c,1
                                                   8223ff18-d538-11e5-80ff-b0086ec8f4cd,1
                                                   be2ac525-3909-11e6-a224-56a308185daf,1      

如何将表格转换为提供的结构?

【问题讨论】:

    标签: sql google-bigquery nosql


    【解决方案1】:

    array_agg() 与结构一起使用怎么样?

    select userid, article_count, test_count, total_articles, total_tests,    
           array_agg(struct(exam_id as 'exam_id', exam_score as 'exam_score')) as exams
    from t
    group by userid, article_count, test_count, total_articles, total_tests
    

    【讨论】:

    • array_agg() 不适用于 BigQuery 的旧 sql 格式。遗留 sql hack 可能吗?
    • @Prabhjot 。 . .使用标准 SQL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多