获取sql中的每小时计数

时间:2017-07-30 11:24:56

标签: mysql sql

我有以下列的表

id(varchar)tnstime(timestamp)status(varchar)和status包含已完成,已提交或失败的固定值。

如何在过去24小时内获得计数

10 - 11  --- completed 110 submited 24 failed 3   
11 - 12  --- completed 125 submited 36 failed 4
13 - 14  --- completed 156 submited 37 failed 8
15 - 16  --- completed 178 submited 26 failed 3
17 - 18  --- completed 179 submited 29 failed 6​

2 个答案:

答案 0 :(得分:0)

这将为您提供不同行的不同状态,如果您希望结果在同一行,我担心您必须编写一个小程序

// Creating a FTP Request
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.KeepAlive = true;

// Enter FTP Server Credentials
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true; // Gets or sets the behaviour of a client application's data transfer process
request.UseBinary = true;
request.EnableSsl = false;

// Fetch the Response and read it into a MemoryStream object
FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

// Save the file to the local directory
using (FileStream writer = new FileStream(localDirectory + fileName, FileMode.Create))
{
    long length = response.ContentLength;
    int bufferSize = 2048;
    int readCount;
    byte[] buffer = new byte[2048];

    readCount = responseStream.Read(buffer, 0, bufferSize);

    while (readCount > 0)
    {
        writer.Write(buffer, 0, readCount);
        readCount = responseStream.Read(buffer, 0, bufferSize);
    }
}

reader.Close();
response.Close();

答案 1 :(得分:0)

假设您的表格按以下方式定义:

INSERT INTO t
    (id, tnstime, status)
VALUES
    ('A0', addtime(current_date, time '03:23:00'), 'failed'),
    ( '1', addtime(current_date, time '10:00:00'), 'completed'),
    ( '2', addtime(current_date, time '10:03:00'), 'failed'),
    ( '3', addtime(current_date, time '10:05:00'), 'completed'),
    ( '4', addtime(current_date, time '10:06:00'), 'submited'),
    ( '5', addtime(current_date, time '10:07:00'), 'completed'),
    ( '6', addtime(current_date, time '11:03:00'), 'completed'),
    ( '7', addtime(current_date, time '11:04:00'), 'completed'),
    ( '8', addtime(current_date, time '11:05:00'), 'completed') ;

......我们有这些数据:

GROUP BY

第一种方法,让SELECT EXTRACT(hour FROM tnstime) AS start_hour, status, count(*) AS c0 FROM t WHERE /* Restrict to doday; the < current_date + interval 1 day may not be needed, because you're supposed to know the future ;-). But just in case time travel were possible. Note that current_date is today at 00:00:00. Note also >= and <. */ tnstime >= current_date AND tnstime < current_date + interval 1 day GROUP BY start_hour, status ORDER BY start_hour, status ; 小时和状态......

q

这将提供以下结果:

start_hour | status    |       c0
---------: | :-------- | -------:
         3 | failed    |        1
        10 | completed |        3
        10 | submited  |        1
        10 | failed    |        1
        11 | completed |        3

现在我们透视这些数据,(请注意,子查询ORDER BY是上一个查询,减去concat)并添加一些格式化细节(lpadSELECT concat(lpad(start_hour, 2, '0'), ' - ', lpad(start_hour + 1, 2, '0')) AS hour_interval, coalesce(sum(CASE WHEN status = 'completed' THEN c0 END), 0) AS completed, coalesce(sum(CASE WHEN status = 'submited' THEN c0 END), 0) AS submited, coalesce(sum(CASE WHEN status = 'failed' THEN c0 END), 0) AS failed FROM (SELECT EXTRACT(hour FROM tnstime) AS start_hour, status, count(*) AS c0 FROM t WHERE tnstime >= current_date AND tnstime < (current_date + interval 1 day) GROUP BY start_hour, status ) AS q GROUP BY hour_interval ORDER BY hour_interval ; ,...)使其看起来与您的请求尽可能相似:

coalesce

请注意,nulls函数会将103(无条目)转换为0.

您将获得以下结果:

hour_interval | completed | submited | failed
:------------ | --------: | -------: | -----:
03 - 04       |         0 |        0 |      1
10 - 11       |         3 |        1 |      1
11 - 12       |         3 |        0 |      0

您可以在 dbfiddle here

查看所有内容

参考: