我有两个表,两个表都有类似的数据,包括唯一的主机名和不同的域名以及操作系统。一个拥有所有主机名和相应的域和操作系统,另一个只有接收更新的设备的主机名和域。我尝试过内连接和语句。我是代码的新手,结果很奇怪。
我需要根据操作系统和操作系统角色(即服务器,具有通用名称的工作站,如Winders 2012 Server,Winders 10等)查找唯一域中总主机名的出现百分比。问题I我试图解决的问题是找出特定域内有多少设备按特定角色在操作系统中获得特殊更新。我想知道从域的可能服务器安装了多少台服务器。查看所需的结果。
使用MySQL 5.7.18,UTF8 charset
表1:
mysql> select Name,domain,OperatingSystem from ad_computers;
+-------------+----------------+---------------------------------+
| Name | domain | OperatingSystem |
+-------------+----------------+---------------------------------+
| NJ03DC01 | smallscomp.com | Winders Server 2008 R2 Standard |
| CORPDC01 | smallscomp.com | Winders Server 2008 R2 Standard |
| CORPWEB1 | anothercom.com | Winders Server 2008 Standard |
| MN1FP1 | stupidsism.com | Winders Server 2008 Enterprise |
| CORPFAS1 | stupidsism.com | Winders Server 2008 Standard |
| CORPSOFTLIC | saturdayis.com | Winders Server 2012 R2 |
| NJFP1 | saturdayis.com | Winders Server 2008 |
| NY1FP2 | anothercom.com | Winders Server 2012 R2 |
| BOSPD-PC5 | anothercom.com | Winders 8.1 Professional |
| NYPD-PCO4 | anothercom.com | Winders 8.1 Professional |
| NYPD-SERV | anothercom.com | Winders Server 2012 R2 |
| ODLPD-PCO8 | saturdayis.com | Winders 10 Enterprise |
+-------------+----------------+---------------------------------+
12 rows in set (0.00 sec)
表2:
mysql> select Name,domain from installed_soft;
+-------------+----------------+
| Name | domain |
+-------------+----------------+
| CORPDC01 | smallscomp.com |
| CORPWEB1 | anothercom.com |
| MN1FP1 | stupidsism.com |
| CORPFAS1 | stupidsism.com |
| CORPSOFTLIC | saturdayis.com |
| NY1FP2 | anothercom.com |
| NYPD-PCO4 | anothercom.com |
| NYPD-SERV | anothercom.com |
| ODLPD-PCO8 | saturdayis.com |
+-------------+----------------+
9 rows in set (0.00 sec)
针对角色类型服务器所需的结果,即 - 例如'%Server%':
+----------+----------------+--------------------+------------------+--------------+
| OS Type | domain | TotalHostsPossible | TotalHostsInstld | PercentInstl |
+----------+----------------+--------------------+------------------+--------------+
| Server | smallscomp.com | 2 | 1 | 50 |
| Server | anothercom.com | 3 | 2 | 67 |
| Server | stupidsism.com | 2 | 2 | 100 |
| Server | saturdayis.com | 2 | 1 | 50 |
+----------+----------------+--------------------+------------------+--------------+
我尝试了各种各样的事情,我不认为下面的代码是完整的,甚至可能是错的。我尝试了一个内连接也有类似的结果。
SELECT ad_computers.Domain,
count(installed_soft.name) as TotHostInstld,
1.0 * COUNT(installed_soft.Name) /
(Select COUNT(distinct ad_computers.Name)
from ad_computers
where ad_computers.OperatingSystem like '%Server%') * 100 as PercentInstl
FROM ad_computers,installed_soft
where ad_computers.OperatingSystem like '%Server%'
and ad_computers.name=installed_soft.name
GROUP BY domain;
有办法做到这一点吗?只要我获得数据,我就不关心性能或格式。表1有大约50k记录,表2有大约3k记录。我的测试查询需要大约10秒才能运行。我通常擅长搞清楚事情,但任何帮助都会受到赞赏。 (我不是一个SQL脑力劳动者,只是一个试图使用SQL的安全人员。)
答案 0 :(得分:0)
请尝试以下代码:
SELECT ad_computers.Domain,COUNT(distinct ad_computers.Name) as TotalHostsPossible,count(installed_soft.name) as TotHostInstld,
1.0 * COUNT(installed_soft.Name) / COUNT(distinct ad_computers.Name)*100 as PercentInstl
FROM ad_computers inner join installed_soft
on ad_computers.name=installed_soft.name
where ad_computers.OperatingSystem like '%Server%'
GROUP BY domain;
答案 1 :(得分:0)
尝试一下:
SELECT
IF(a.OperatingSystem like '%Server%' ,'Server','Workstation') as `OS Type`,
a.domain,
count(distinct a.name) as TotalHostsPossible,
count(distinct b.name) as TotHostInstld,
count(distinct a.name) / count(distinct b.name) * 100 as PercentInstl
FROM ad_computers a
JOIN installed_soft b
ON a.name = b.name
where a.OperatingSystem like '%Server%'
GROUP BY a.domain;