0

我有一个表messages,其中包含一个列message_internaldate。现在我想计算几个月内特定时间段(一天中的每个小时)内的消息。通过拥有大量子查询(24),我可以设法获得每小时消息的总和,但我希望有一种更聪明的方法来做到这一点。除了时间段发生变化外,子查询类似。有什么建议么?

例如前两个小时

SELECT T1, T2 FROM 
(
SELECT sum(T1c) as T1 FROM
    (
    SELECT strftime('%H:%M',message_internaldate) AS T1s ,count(*) as T1c FROM messages WHERE
    message_internaldate BETWEEN '2005-01-01 00:00:00' AND '2012-12-31 00:00:00'
    AND strftime('%H:%M',message_internaldate) BETWEEN '01:00'AND '01:59'
    GROUP BY strftime('%H:%M',message_internaldate)
    )
)
,
(
SELECT sum(T2c) as T2 FROM
    (
    SELECT strftime('%H:%M',message_internaldate) AS T2s ,count(*) as T2c FROM messages WHERE
    message_internaldate BETWEEN '2005-01-01 00:00:00' AND '2012-12-31 00:00:00'
    AND strftime('%H:%M',message_internaldate) BETWEEN '02:00'AND '02:59'
    GROUP BY strftime('%H:%M',message_internaldate)
    )
)
...
4

1 回答 1

1

您的问题是您希望将各个小时作为列。

要将它们作为行获取,请尝试如下查询:

SELECT strftime('%H', message_internaldate) AS hour,
       strftime('%H:%M', message_internaldate) AS Ts,
       COUNT(*) AS Tc
FROM messages
WHERE message_internaldate BETWEEN '2005-01-01 00:00:00' AND '2012-12-31 23:59:59'
GROUP BY 1, 2
于 2013-03-18T10:59:35.630 回答