在sqlite中正确使用嵌套选择

时间:2014-04-23 12:09:17

标签: sql sqlite android-sqlite


我正在尝试创建一个查询来聚合来自数据库的点击
包含每次点击的时间和日期条目
我被困住了,因为我的查询没有“编译”
我希望在特定时间之间汇总点击次数并将其显示在一个类别中
(早上,中午,晚上,晚上)我该怎么办? - 这就是我现在所做的

SELECT n
FROM click_history
Where (SELECT count(*) from click_history where click_history_hour BETWEEN '08:00:00' AND '12:00:00') as n

1 个答案:

答案 0 :(得分:2)

如果您想按小时聚合,那么我希望看到group by。也许是这样的:

select (case when click_history_hour < '08:00:00' then 'time1'
             when click_history_hour < '12:00:00' then 'time2'
             else 'time3'
        end) as whichtime, count(*) as n
from click_history
group by (case when click_history_hour < '08:00:00' then 'time1'
               when click_history_hour < '12:00:00' then 'time2'
               else 'time3'
        end);

当然,期间的名称和定义取决于您。