0

我有一张表格,记录了一些地点的一些访问信息以及时间,也许还有一些关于这个地方的评论,比如:

+---------+---------+------------+-----------------------+---------------------+
| visitId | userId  | locationId | comments              | time                |
+---------+---------+------------+-----------------------+---------------------+
|       1 |    3    |     12     | It's a good day here! | 2012-12-12 20:50:12 |
+---------+---------+------------+-----------------------+---------------------+

我想做的是按小时计算访问组的数量,我想这样生成结果:

+------------+-----+-----+-----+-----+-----+-----+-------+------+
| locationId |  0  |  1  |  2  |  3  |  4  |  5  |  ...  |  23  |
+------------+-----+-----+-----+-----+-----+-----+-------+------+
|      12    |  15 |  12 |  34 |  67 |  78 |  89 |  ...  |  34  | 
+------------+-----+-----+-----+-----+-----+-----+-------+------+

我怎样才能做到这一点?我想评估一整天的访问量差异。

4

3 回答 3

1

这将为您提供每个 locationid 每小时的访问次数

SELECT locationId, HOUR(time), COUNT(*)
FROM table
GROUP BY locationId, HOUR(time)
于 2013-03-20T12:39:05.803 回答
1

未经测试,我认为这将起作用:

select locationid, hour(time) as hour, count(distinct userid) as usercnt
from table_1
group by locationid, hour;

它不会按照您的建议进行转置,但数据是相同的

于 2013-03-20T12:42:34.193 回答
1

尝试使用如下查询:

SELECT  location_id, sum(case when HOUR(time) = 0 then 1 else 0) as '0', 
        sum(case when HOUR(time) = 1 then 1 else 0) as '1',-- till 23
FROM table
GROUP BY location_id
于 2013-03-20T12:46:02.687 回答