希望有人可以提供一些建议 我试图获取一个SQLite查询来计算时间范围内值的出现时间为15分钟并返回非重复计数
我的数据看起来像这样
--------- ---------- 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
使用我当前的选择我得到一个结果,但它没有考虑Connection_Time和Last Accessed之间的时间跨度
select datetime((strftime('%s', currentUsers.Connection_Time) / 300) * 300, 'unixepoch') > > interval, count(distinct(UserName)) cnt from currentUsers group by interval order by interval
任何帮助将不胜感激。
答案 0 :(得分:0)
要计算所有间隔,您需要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;