基于 whitin 时间线的 sqlite 计数结果

sqlite count results based whitin timeline

希望有人能提供一些建议 我正在尝试获取一个 SQLite 查询来计算一个值在 15 分钟间隔和 return distinct count

的时间范围内的出现次数

我的数据看起来像这样

---------DATA----------

Connection_Time         Last_Accessed               UserName   
2015-01-12 06:00:00     2015-01-12 07:00:00          JamesG    
2015-01-12 06:10:00     2015-01-12 06:12:00          JohnH
2015-01-12 06:32:00     2015-01-12 07:00:00          JohnH

这是我想要的结果

Result                      count
"2015-01-12 06:00:00"         2
"2015-01-12 06:15:00"         1
"2015-01-12 06:30:00"         2
"2015-01-12 06:45:00"         2
"2015-01-12 07:00:00"         2

用我当前的 select 我得到了一个结果,但它没有考虑 Connection_Time 和上次访问

之间的时间跨度
select datetime((strftime('%s', currentUsers.Connection_Time) / 300) * 300, 'unixepoch') >    >     interval,
count(distinct(UserName)) cnt
from currentUsers
group by interval
order by interval

如有任何帮助,我们将不胜感激。

要计算所有间隔,您需要 CTE(这需要足够新的 SQLite 版本):

WITH RECURSIVE
min_time(t) AS (
  SELECT datetime((strftime('%s',
                            (SELECT MIN(Connection_Time)
                             FROM currentUsers)
                  ) / 300) * 300,
                  'unixepoch')
),
max_time(t) AS (
  SELECT datetime((strftime('%s',
                            (SELECT MAX(Last_Access)
                             FROM currentUsers)
                  ) / 300) * 300,
                  'unixepoch')
),
intervals(t) AS (
  SELECT t FROM min_time
  UNION ALL
  SELECT datetime(t, '+15 minutes')
  FROM intervals
  WHERE t <= (SELECT t FROM max_time)
)
SELECT t AS interval,
       (SELECT COUNT(DISTINCT UserName)
        FROM currentUsers
        WHERE Connection_Time < datetime(intervals.t, '+15 minutes')
          AND Last_Accessed >= intervals.t
       ) AS Count
FROM intervals;