从两列中查找唯一数据

时间:2018-12-01 16:48:52

标签: mysql

我有一个SQL表,其中更新了许多节点的运行状况检查报告,下面是示例表

Node Check    Status
   1 Network  Compliant
   1 Disk     Compliant
   1 Services Non-Compliant
   1 Memory   Compliant
   2 Network  Compliant
   2 Disk     Compliant
   2 Services Compliant    
   3 Memory   Compliant    
   3 Network  Compliant    
   3 Disk     Compliant    
   3 Services Compliant    
   3 Memory   Compliant    
   4 Network  Compliant    
   4 Disk     Compliant    
   4 Services Compliant    
   4 Memory   Compliant    
   5 Network  Non-Compliant    
   5 Disk     Compliant    
   5 Services Compliant    
   5 Memory   Compliant    
   6 Network  Compliant    
   6 Disk     Compliant    
   6 Services Compliant    
   6 Memory   Compliant  

我的要求是获取以下信息

计算不同节点的总数,然后,即使在给定日期某节点的一项检查失败,也将该节点标记为“不合规”,并且仅当给定日期的所有检查都为“合规”时,才将该节点标记为“不合规”。并显示以下信息

Desired Result

谢谢

3 个答案:

答案 0 :(得分:0)

我们首先可以确定派生表中每个Node的“完全合规”状态。然后,我们可以使用此结果集获取总数。

SELECT
  COUNT(*) AS total_nodes, 
  SUM(fully_compliant = 0) AS Non_compliant_nodes, 
  SUM(fully_compliant = 1) AS compliant_nodes 
FROM 
(
  SELECT
    Node,
    CASE WHEN SUM(Status = 'Compliant') = COUNT(*) THEN 1
         ELSE 0
    END AS fully_compliant 
  FROM your_table
  GROUP BY Node 
) AS dt 

结果

| total_nodes | Non_compliant_nodes | compliant_nodes |
| ----------- | ------------------- | --------------- |
| 6           | 2                   | 4               |

View on DB Fiddle

答案 1 :(得分:0)

以下是先前问题的答案:

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(node INT NOT NULL
,metric ENUM('Network','Disk','Services','Memory') NOT NULL
,status ENUM('Compliant','Non-Compliant')
,PRIMARY KEY(node,metric)
);

INSERT INTO my_table VALUES
(1,'Network','Compliant'),
(1,'Disk','Compliant'),
(1,'Services','Non-Compliant'),
(1,'Memory','Compliant'),
(2,'Network','Compliant'),
(2,'Disk','Compliant'),
(2,'Services','Compliant'),
(2,'Memory','Compliant'),
(3,'Network','Compliant'),
(3,'Disk','Compliant'),
(3,'Services','Compliant'),
(3,'Memory','Compliant'),
(4,'Network','Compliant'),
(4,'Disk','Compliant'),
(4,'Services','Compliant'),
(4,'Memory','Compliant'),
(5,'Network','Non-Compliant'),
(5,'Disk','Compliant'),
(5,'Services','Compliant'),
(5,'Memory','Compliant'),
(6,'Network','Compliant'),
(6,'Disk','Compliant'),
(6,'Services','Compliant'),
(6,'Memory','Compliant');

SELECT COUNT(DISTINCT x.node) total
     , COUNT(DISTINCT CASE WHEN y.node IS NULL THEN x.node END) compliant
     , COUNT(DISTINCT CASE WHEN y.node IS NOT NULL THEN x.node END) `non-compliant` 
  FROM my_table x 
  LEFT 
  JOIN my_table y
    ON y.node = x.node 
   AND y.status = 'non-compliant';
+-------+-----------+---------------+
| total | compliant | non-compliant |
+-------+-----------+---------------+
|     6 |         4 |             2 |
+-------+-----------+---------------+

答案 2 :(得分:-1)

select count(distinct Node) as TotalNodes, SUM(DISTINCT CASE WHEN status='non_compliant' THEN 1 ELSE 0 END) NonComp,SUM(DISTINCT CASE WHEN status='compliant' THEN 1 ELSE 0 END) Comp from Table;