来自 2 个不同表的值出现的 Mysql 百分比

标签 mysql sql percentage

我有两个表,它们都有相似的数据,包括唯一的主机名和不同的域名以及操作系统。一个具有所有主机名和相应的域和操作系统,另一个只有接收更新的设备的主机名和域。我试过内部联接和 where 语句。我是代码新手,结果却很奇怪。

我需要根据操作系统和操作系统角色(即服务器、具有通用名称的工作站,如 Winders 2012 Server、Winders 10 等)查找唯一域中主机名总数的出现百分比。我的问题我试图解决的是找出特定域中有多少设备按百分比在操作系统的特定角色中收到了特殊更新。我想从一个域的可能服务器中安装多少台服务器。查看所需的结果。

使用 MySQL 5.7.18,UTF8 字符集

表 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 的安全人员。)

最佳答案

试试下面的代码:

    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;

关于来自 2 个不同表的值出现的 Mysql 百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43698482/

相关文章:

SQL - 将时间戳规范化为营业时间

mysql - 在 Logstash 的 JDBC 输入插件中设置 MySQL conf

ios - 如何以父 View 大小的百分比设置 View 的高度

CSS 边距表现奇怪

c# - Entity Framework 为我使用变量的每个地方创建一个新列

java - 如何在 JDBC 应用程序中使用动态对象

mysql - 随着时间的推移,存储、持续跟踪测量结果的最佳实践是什么?

mysql - 基于列合并表

sql - PostgreSql 表中 UUID、CHAR 和 VARCHAR 之间的性能差异?

css - 100% 宽度的像素偏移