使用AWS Athena查询aws_application表。
表有如下内容
ID | Name
server1 | Word
server1 | Excel
server2 | Word
server2 | Excel
server3 | Word
server3 | Excel
server3 | Notepad
我正在寻找可以列出未安装“记事本”的服务器ID(在此示例中)的SQL查询。结果应显示。
ID
server1
server2
我是新手,到目前为止,我只能显示哪个服务器装有记事本。我以为我可以以某种方式将表加入自身并减去以尝试获得唯一的ID。
上面的示例是通用的,但更易于解释。我可以运行以下命令
select distinct resourceid
from aws_application
where name = 'Excel'
or name = 'Word'
group by resourceid
总共获得108台服务器。
如果我跑步
select distinct resourceid
from aws_application
group by resourceid
我获得了116台服务器的唯一计数。我想返回数字8。
当然,这里有成千上万的行,因为表中的每一行代表包装盒上安装的不同应用程序exe。
我正在寻找可以告诉我有多少服务器(在此示例中)没有安装“记事本”的SQL查询。
您可以使用两种聚合级别:
select count(*)
from (select id, sum(case when name = 'Notepad' then 1 else 0 end) as num_notepad
from aws_application a
group by id
) s
where num_notepad = 0;
如果您想要列表而不是计数:
select id,
from aws_application a
group by id
having sum(case when name = 'Notepad' then 1 else 0 end) = 0;
不过,更通常的情况是,您将有一张servers
桌子。然后,您将执行以下操作:
select count(*)
from servers s
where not exists (select 1
from aws_application a
where a.userid = s.userid and
a.name = 'Notepad'
);
或对于列表,使用select s.*
代替。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句