我有一个日志表:
user_id,path_id,poi_id,时间戳,event_type

就像博物馆中的信标数据库一样,它却是音频导览系统的一部分,人们可以在同一地点选择不同的路径。

例:

John, Strolling at the Zoo Tour,Beacon_1, 2015-05-05 11:49, "WentAcross"
Zoe, An adventure in the Lion's Lair, Beacon_15, 2015-05-05 11:50, "WentAcross"
John, Strolling at the Zoo Tour, Beacon_3, 2015-05-05 11:50, "WentAcross"
John, Strolling at the Zoo Tour, Beacon_4, 2015-05-05 11:51, "WentAcross"
Kyle, Strolling at the Zoo Tour,Beacon_4, 2015-05-05 11:51, "WentAcross"
Zoe, An adventure in the Lion's Lair, Beacon_15, 2015-05-05 11:50, "WentAcross"
Zoe, An adventure in the Lion's Lair, Beacon_1, 2015-05-05 11:51, "WentAcross"
John, Strolling at the Zoo Tour, Beacon_6, 2015-05-05 11:51, "WentAcross"
John, Strolling at the Zoo Tour, Beacon_5, 2015-05-05 11:52, "WentAcross"
John, Strolling at the Zoo Tour, Beacon_2, 2015-05-05 11:53, "WentAcross"


我想知道路径中的时间戳首次为某个用户触摸了poi多少次。

在此示例中,结果应为:


Beacon_1 1(约翰感动第一点,对Zoe而言,并不是
首先...可以是路径交叉,或只是在其他地方连接)
Beacon_15 2(Zoe和Kyle在Lion's
巢穴之旅)
Beacon_4 1(在“漫步”中被当作第一点
佐伊(Zoe)的动物园之旅-她似乎在途中连接了)


每隔一个Beacon_ID应读取零。

我如何建立这样的查询?

最佳答案

作为您的表列,诸如“名称”,“信标”,“时间戳”和表名“事件”之类的东西……我想到一个临时表……诸如此类:

SELECT GROUP_CONCAT(evt.name), tmp_evt.beacon, tmp_min_ts, COUNT(1) as touching
FROM events evt
INNER JOIN (
   SELECT evt.beacon AS beacon, MIN(timestamp) AS min_ts
   FROM events evt
   GROUP BY evt.beacon
 ) tmp_evt ON (tmp_evt.min_ts = evt.timestamp AND tmp_evt.beacon = evt.beacon)
GROUP BY tmp_evt.beacon

10-04 10:21