Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite 避免重复子查询_Sqlite - Fatal编程技术网

Sqlite 避免重复子查询

Sqlite 避免重复子查询,sqlite,Sqlite,我有一个表messages,其中包含一列message\u internaldate。现在,我想计算几个月内特定时间段(每天每小时)内的消息数。我可以通过大量的子查询(24)来获得每小时的消息总数,但我希望有一种更聪明的方法来做到这一点。子查询类似,只是时间段发生了变化。有什么建议吗 e、 前两个小时 SELECT T1, T2 FROM ( SELECT sum(T1c) as T1 FROM ( SELECT strftime('%H:%M',message_interna

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

e、 前两个小时

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)
    )
)
...

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

要将它们作为行,请尝试以下查询:

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

谢谢我补充道:按小时从(您的查询)组中选择hour,sum(Tc)作为SumOfMails,这正是我最初想要做的!