我的数据库中有一个大型日志表,我需要从中提取一些信息。
我正在努力的输出是:
日 - 尝试 - 失败的尝试
我的 table 看起来像这样;
LogID - Timestamp - Sender - Receiver - SecondsConEst - InOut - ErrorMsg - MsgID
要提取此错误,我需要计算 SecondsConEst >= 1 且 InOut = Out 且 MsgID 已重复 8 次或更多次的 MsgID。
目前我有:
SELECT date(timestamp) as Day, count(MsgID) as attempts
FROM database.log
where Receiver like 'AAB%' and out = 'Out' and (SecondsConEst >= '1' and ErrorMsg != '')
group by MsgID having count(messageid) >= 8 ;
现在这给了我
Day Attempts
2016-02-15 9
2016-02-15 8
但我想合并这个。如果尝试次数达到或超过 8 次,则可以称为“失败”,应计为失败,显示当天失败的总数。
我尝试过使用
count(case when count(MsgID >= 8) then 1 else NULL end)
在我的选择中,但这给了我“无效使用组功能”。
我当然也想显示总尝试次数,是否可以为此进行某种内部联接?喜欢
SELECT (distinct MsgID) inner join where...
任何指针都会很棒。
最佳答案
我认为你需要另一个级别的聚合:
SELECT day, SUM(attempts >= 8) as Failures, COUNT(*) as Total
FROM (SELECT date(MIN(timestamp)) as Day, count(MsgID) as attempts
FROM database.log
WHERE Receiver like 'AAB%' and out = 'Out' and
(SecondsConEst >= '1' and ErrorMsg <> '')
GROUP BY MsgID
)
GROUP BY day;
请注意,此版本明确为每个消息 ID 选择最小时间戳。这可以处理消息分散在多天内的情况。
关于mysql - 构造 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35630652/