【问题标题】:athena query to collapse sparse matrix / columnar data雅典娜查询折叠稀疏矩阵/列数据
【发布时间】:2021-11-16 07:29:08
【问题描述】:

假设我在 Amazon Athena 中有以下表格 table1(从技术上讲,在 Glue 中):

torg, foo, bar, x1, x2, x3, baz
-------------------------------
t1, 1, NULL,   NULL, NULL, NULL, goober
t2, 1, NULL,   NULL, NULL, NULL, NULL  -- want to ignore this row entirely, even though foo=1
t3, 1, tronic, NULL, NULL, NULL, NULL
...
t4, 2, NULL,   NULL, NULL, NULL, horse
t4, 2, zebra,  NULL, NULL, NULL, NULL
...

是否有一个 Athena 查询可以 group by foo(在此示例中为 foo=1),但忽略每一行的 NULL 列值,这样结果看起来像?

foo, bar, baz
----------------
1, tronic, goober

对于上下文,我在不同时间将分区镶木地板写入 S3。使用 Athena 查询这些数据真是太棒了,但我很难想象 Athena 是否适合将这些列数据“折叠”成按特定列(或一组列)分组的更多“逐行”数据)。

虽然我可能会在 pandas 中进一步处理这些数据,并且可以在那里“折叠”它,但我希望 Athena 中可能存在查询模式。或者,也许我在想这完全是错误的......

非常感谢任何见解。

【问题讨论】:

    标签: sparse-matrix parquet amazon-athena


    【解决方案1】:

    如果每个组只有一个值,则可以使用max(或min)和group by

    WITH dataset(foo,bar, baz) AS (
       VALUES 
        (1,NULL,'tronic'),
        (1,'goober',NULL),
        (1,NULL, NULL)
     ) 
     
    SELECT foo, max(bar) bar, max(baz) baz
    FROM dataset
    GROUP BY foo
    

    输出:

    foo bar baz
    1 goober tronic

    【讨论】:

    • 优雅,简单,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2013-08-27
    • 2023-01-04
    • 1970-01-01
    相关资源
    最近更新 更多