2

我在表中有以下(示例)数据

值、日期、设备

我使用这些数据进行简单的 SLA 计算,即在工作时间有多少次数据低于某个阈值。

目前我运行以下两个查询,如下所示

select count(*), DATE(times) from SLA where device='some-dev' and DATE(times) between '2013-05-01' and '2013-05-31' and TIME(times) between '06:00:00' and '18:00:00' and value <150 group by DATE(times);

select count(*), DATE(times) from SLA where device='some-dev' and DATE(times) between '2013-05-01' and '2013-05-31' and TIME(times) between '06:00:00' and '18:00:00' group by DATE(times);

所以有两个查询,一个没有 value < number,一个有。

几个问题,有没有办法将这两个都放入一个查询中,这样我就可以看到总数据点和小于阈值的数据点的计数?基本上返回三列,一列有计数,一列计数低于阈值,还有日期

除此之外,是否有一种简单的方法可以告诉 mysql 在返回数据时忽略周末?

谢谢,

4

5 回答 5

3

使用UNION运算符。

更新后的查询将如下所示:

(select count(*), DATE(times) from SLA where device='some-dev' and DATE(times) between '2013-05-01' and '2013-05-31' and TIME(times) between '06:00:00' and '18:00:00' and value <150 group by DATE(times))
UNION
(select count(*), DATE(times) from SLA where device='some-dev' and DATE(times) between '2013-05-01' and '2013-05-31' and TIME(times) between '06:00:00' and '18:00:00' group by DATE(times));
于 2013-06-13T15:59:37.727 回答
2

如果,如您所写,您想要三列(两个计数和一个日期),请使用子选择而不是联合:

select 
(select count(*) from SLA where device='some-dev' and DATE(times) = DATE(s.times) and  TIME(times) between '06:00:00' and '18:00:00' and value <150),
count(*), 
DATE(s.times) 
from SLA s
where device='some-dev' and DATE(times) between '2013-05-01' and '2013-05-31' and TIME(times) between '06:00:00' and '18:00:00' group by DATE(times)

我没有测试上面的查询,所以它可能需要一些调整,但是请看一个简化的工作示例

编辑:刚刚用(甚至)更有意义的查询更新了链接的小提琴(再一次)。

于 2013-06-13T16:27:24.530 回答
1

我更喜欢 ms-sql,但大概你可以这样做:

select count(*), DATE(times), SUM(CASE WHEN value <150  THEN 1 ELSE 0 END)
from SLA
where device='some-dev'
and DATE(times) between '2013-05-01' and '2013-05-31'
and TIME(times) between '06:00:00' and '18:00:00'
group by DATE(times);

在mysql中。

于 2013-06-14T09:41:16.053 回答
1
select count(*), DATE(time) from SLA where (device='some-dev' and DATE(time) between '2013-05-01' and '2013-05-31' and TIME(times) between '06:00:00' and '18:00:00' and value <150 group by DATE(time)) or (device='some-dev' and DATE(times) between '2013-05-01' and '2013-05-31' and TIME(time) between '06:00:00' and '18:00:00' group by DATE(time));
于 2017-10-29T11:39:39.647 回答
0

怎么样:

select count(*), DATE(times) from SLA 
where (device='some-dev' and DATE(times) between '2013-05-01' and '2013-05-31' and TIME(times) between '06:00:00' and '18:00:00' and value <150 group by DATE(times))
or (device='some-dev' and DATE(times) between '2013-05-01' and '2013-05-31' and TIME(times) between '06:00:00' and '18:00:00' group by DATE(times));
于 2013-06-13T16:00:40.743 回答