我想比较同一张表中的行,并显示前几天数据发生了什么变化
例如:如果我的指标表如下
id | Server | Component | Status | Date
------+----------+-------------------------------------------
1 | Node1 | Service | Compliant | 10-1-2018
2 | Node1 | IP | Compliant | 10-1-2018
3 | Node2 | Service | Non-Compliant | 10-1-2018
4 | Node2 | IP | Compliant | 10-1-2018
5 | Node1 | Service | Compliant | 09-1-2018
6 | Node1 | IP | Compliant | 09-1-2018
7 | Node2 | Service | Compliant | 09-1-2018
8 | Node2 | IP | Compliant | 09-1-2018
我想要的结果如下,因为与前一天的状态相比,节点2服务合规性状态已更改(变为不合规)
Node2 | Service | Non-Compliant | 09-1-2018
我想要的基本上是跟踪与前一天的数据相比,节点的合规性状态变化
谢谢
答案 0 :(得分:0)
http://sqlfiddle.com/#!9/1b0c46/1
SELECT t_next.*
FROM t1 t_prev
JOIN t1 t_next
ON t_next.server = t_prev.server
AND t_next.component = t_prev.component
AND t_next.date = t_prev.date + INTERVAL 1 DAY
AND t_next.status != t_prev.status
WHERE t_prev.date = '2018-01-09'