优化Sqlite查询

时间:2014-04-27 21:38:26

标签: sql sqlite android-sqlite


我正在努力寻找解决我遇到的问题的方法,并感谢任何帮助 -
我正在开发一个Android应用程序,其中一部分需要收集用户点击
我使用以下参数存储sqlite表中的每次单击 -
点击ID,点击日期,点击月份,点击年份,然后点击小时
(日期细分用于其他查询)
我的任务 -
我需要通过以下时间显示每日用户点击的时间分段(从他询问的点和24小时后)
早上8-11之间的任何一小时,中午12-17之间的任何一小时,晚上17-22之间的任何一小时
晚上22-8之间的任何一小时(当天早上8点)
我的问题 -
至于现在我的查询将两天之间的时间段统一起来 -
如果我从下午5点开始24小时后看,并且在前一天下午6点有一个记录,那就算是今天(因为我想要分开,所以不应该这样做) ) 在两天之间的时间片之间除非之夜
时间片。
我的SQL查询

SELECT (CASE 
WHEN (click_history_hour > '07:59:59' AND click_history_hour < '12:00:00') THEN '8 - 11' 
WHEN (click_history_hour > '11:59:59' AND click_history_hour < '17:00:00') THEN '12 - 17' 
WHEN (click_history_hour > '16:59:59' AND click_history_hour < '22:00:00') THEN '17 - 22' 
WHEN ((click_history_hour > '21:59:59' AND click_history_hour < '23:59:59') OR (click_history_hour > '00:00:00' AND click_history_hour < '08:00:00')) THEN '22 - 8'
END) 
AS time, COUNT(*) AS quantity 
FROM click_history 
WHERE (click_history_day='25' AND click_history_month='04' AND click_history_year='2014' AND click_history_hour>'00:45:21') 
OR (click_history_day='26' AND click_history_month='04' AND click_history_year='2014') 
GROUP BY (CASE 
WHEN (click_history_hour > '07:59:59' AND click_history_hour < '12:00:00') THEN '8 - 11' 
WHEN (click_history_hour > '11:59:59' AND click_history_hour < '17:00:00') THEN '12 - 17' 
WHEN (click_history_hour > '16:59:59' AND click_history_hour < '22:00:00') THEN '17 - 22' 
WHEN ((click_history_hour > '21:59:59' AND click_history_hour < '23:59:59') OR (click_history_hour > '00:00:00' AND click_history_hour < '08:00:00')) THEN '22 - 8'
END) 
ORDER BY (click_history_month || click_history_day || click_history_year), click_history_hour ASC;

请注意 - 日期是因为这是来自调试会话的实际查询 任何帮助?


修改
我正在检查的测试数据 - enter image description here

查询结果 -
enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

select 
  CASE 
    WHEN click_hour < '09' THEN '23 -  8'
    WHEN click_hour < '12' THEN ' 8 - 11'
    WHEN click_hour < '18' THEN '12 - 17'
    WHEN click_hour < '23' THEN '18 - 22'
    ELSE                        '23 -  8'
  END AS time, 
  COUNT(*) AS quantity 
from 
  (
  SELECT 
    click_history_year||'-'||click_history_month||'-'||click_history_day||' '||click_history_hour  as click_time, 
    substr(click_history_hour,1,2) as click_hour
  FROM click_history
  ) as click_history2
WHERE datetime('now','-1 day') <= click_time
group by time
;

请注意,如果您只使用一列来存储时间戳(我人为创建为click_time),这将更加清晰。您还可以删除现在的4列。要从时间戳获取小时,请使用strftime(click_time, '%H')

另外,我调整了时间范围和比较,因为有些似乎是错误的。也许我只是误解了你的要求。

请注意,您可以将WHERE子句移到子查询中(并消除click_time),但这取决于如何使用代码。它会是这样的:

select 
  CASE 
    WHEN click_hour < '09' THEN '23 -  8'
    WHEN click_hour < '12' THEN ' 8 - 11'
    WHEN click_hour < '18' THEN '12 - 17'
    WHEN click_hour < '23' THEN '18 - 22'
    ELSE                        '23 -  8'
  END AS time, 
  COUNT(*) AS quantity 
from 
  (
  SELECT 
    substr(click_history_hour,1,2) as click_hour
  FROM click_history
  WHERE datetime('now','-1 day') <= click_history_year||'-'||click_history_month||'-'||click_history_day||' '||click_history_hour
  ) as click_history2
group by time
;

这是一个检查语法的SQLfiddle:http://sqlfiddle.com/#!7/992ca/2