簡體   English   中英

如何從 Athena SQL 查詢中的 3 個表中獲取所需數據?

[英]How to get the desired data from 3 tables in Athena SQL query?

表 1 ( aws_complianceitem ) 沒有主鍵,此示例數據:

地位 嚴重性 合規類型 標題 資源ID 地區
合規 低的 安全 2002 補丁 我-76765434 ap-south-2
不合規 高的 審計 2002 KB 補丁 我-76765434 ap-south-2
合規 中等的 安全 2002 KB 補丁 我-98765434 ap-south-1

表 2 ( aws_instanceinformation ) 將ipaddressinstanceid作為唯一鍵,並具有以下示例數據:

計算機名 實例id IP地址 地位 帳戶ID
SD-SDYH-re22 我-76765434 10.33.23.1 合規 887878787654
不合規 我-98765434 10.72.33.1 不合規 098776765478

表 3 ( configinstancestate ) 將ipaddressresourceid作為唯一鍵,並且此示例數據:

資源ID IP地址 實例狀態
我-76765434 10.33.23.1 跑步
我-98765434 10.72.33.1 停止

我需要所有正在運行的 instanceid 的數據。

這是期望的結果:

地位 實例狀態 嚴重性 標題 資源ID 地區 IP地址
合規 跑步 低的 2002 補丁 我-76765434 ap-south-2 10.33.23.1
不合規 跑步 高的 2002 KB 補丁 我-76765434 ap-south-2 10.33.23.1
合規 停止 中等的 2002 KB 補丁 我-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 作為這個查詢的一部分,有空記錄

似乎兩個內部連接 - 具有相關條件 - 做你想做的事:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM