【问题标题】:How to get the desired data from 3 tables in Athena SQL query?如何从 Athena SQL 查询中的 3 个表中获取所需的数据?
【发布时间】:2021-03-29 07:19:16
【问题描述】:

表 1 (aws_complianceitem) 没有主键,此示例数据:

status severity compliancetype title resourceid region
compliant low security 2002 patch i-76765434 ap-south-2
noncompliant high audit 2002 kb patch i-76765434 ap-south-2
compliant medium security 2002 kb patch i-98765434 ap-south-1

表 2 (aws_instanceinformation) 具有 ipaddressinstanceid 作为唯一键,并具有以下示例数据:

computername instanceid ipaddress status accountid
SD-SDYH-re22 i-76765434 10.33.23.1 complianed 887878787654
noncompliant i-98765434 10.72.33.1 non-complianed 098776765478

表 3 (configinstancestate) 具有 ipaddressresourceid 作为唯一键和此示例数据:

resourceid ipaddress instancestate
i-76765434 10.33.23.1 running
i-98765434 10.72.33.1 stopped

我需要所有正在运行的 instanceid 的数据。

这是想要的结果:

status instancestate severity title resourceid region ipaddress
compliant running low 2002 patch i-76765434 ap-south-2 10.33.23.1
noncompliant running high 2002 kb patch i-76765434 ap-south-2 10.33.23.1
compliant stopped medium 2002 kb patch i-98765434 ap-south-1 10.72.33.1

尝试使用以下查询的完全外连接,

SELECT
    t1.status
    , t1.severity
    , t1.title
    , t1.region
    , t1.resourceid
    , t2.ipaddress
    , t2.computername
    , t2.status
    , t3.instancestate
FROM
    aws_complianceitem                      t1
    FULL OUTER JOIN aws_instanceinformation t2
        ON t1.resourceid = t2.instanceid
    FULL OUTER JOIN configinstancestate     t3
        ON t2.ipaddress = t3.resourceid

但是我们过滤了这个结果,我们作为这个查询的一部分使用 instancestate=blank,有空记录

【问题讨论】:

    标签: sql amazon-web-services inner-join presto amazon-athena


    【解决方案1】:

    看起来像两个内部连接 ​​- 具有相关条件 - 做你想做的事:

    select ac.*, c.*
    from aws_complianceitem ac
    inner join aws_instanceinformation ai 
        on ai.resourceid = ac.resourceid
    inner join configinstancestate c
        on c.resourceid = ac.resourceid and c.ipaddress = ac.ipaddress
    

    【讨论】:

    • 不支持此类型的查询(服务:AmazonAthena;状态代码:400;错误代码:InvalidRequestException;请求 ID:xxxxxxxxxxxxxxxxxxxx-xxxxxxxxx-1exxxxxxxxxx)使用此查询在 athena 中出现上述错误
    • @Gandharkhaladkar:我刚刚修正了一个错字(尾随 and)。现在效果更好了吗?
    猜你喜欢
    • 2021-01-07
    • 2012-07-21
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多