簡體   English   中英

構造SQL查詢

[英]Construct SQL-query

我在數據庫中有一個很大的日志表,需要從中提取一些信息。

我正在努力的輸出是:

日-嘗試-嘗試失敗

我的桌子看起來像這樣;

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,count(case when attempts > 8 then 1 end) as cntFailed FROM(
    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 );
GROUP BY Day

如果我對您的理解正確,那么您所缺少的只是另一個選擇來包裝您的選擇。

我認為您需要另一個聚合級別:

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選擇最小時間戳。 當消息在多天之間分割時,這種情況會處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM