好的,如果没有在白板上或其他东西上画出来,这很难解释......但是在这里。我试图尽可能清楚,但如果这没有意义,请告诉我....
我有一个MS Access项目,处理来自多个源对象或“SOURCES”的时间序列数据集,以及多个观察点或“RECEIVERS”,并根据时间和空间接近度识别感兴趣的事件。这给了我一个可能相关事件的触发器表,其中包含以下字段。
CORRELATION_ID
RECEIVER_EVENT_ID
RECEIVER_NAME
RECEIVER_START_DATETIME
RECEIVER_END_DATETIME
SOURCE_EVENT_ID
SOURCE_NAME
SOURCE_START_DATETIME
SOURCE_END_DATETIME
因为我可以在重叠时间或彼此接近的时间发生多个源和接收器触发器,所以我得到了大量的触发器列表,我想通过进一步对其进行分组来优化这个触发器列表。标准。
我想为源事件之间的最大允许时间间隔MAX_SOURCE_GAP和接收器事件MAX_RECEIVER_GAP之间的最大允许间隙指定2个标准。 GAP是从一个触发的开始时间减去另一个触发的结束时间计算的。
如果事件在此间隙范围内,则需要对它们进行分组,结果组记录必须存储最早事件的开始时间和最新事件的结束时间。对于RECEIVER事件,RECEIVER_NAME必须相同。 (即我不想将来自不同RECEIVERS的事件分组,因为我仍然希望得到相关RECEIVER<> SOURCE事件的列表)对于SOURCE事件,事件必须由同一个Receiver拾取,换句话说, RECEIVER_NAME必须再次相同。我还希望记录返回分组的源的名称列表。为此,我想我可以实现Allen Browne的ConcatRelated()函数。
已更新 所需的第三个条件定义了分组的源事件与分组的接收器事件MAX_SOURCE_TO_RECEIVER_DELAY之间的关系。这是可以触发接收器的源的开始时间之后的最大允许时间延迟。换句话说,startTime_receiver - startTime_source< = MAX_SOURCE_TO_RECEIVER_DELAY。接收器也可以在源之前不触发,因此startTime_receiver< startTime_source。
我认为基本上这需要几步。至少一个子查询对SOURCE事件进行分组。至少一个子查询,用于对RECEIVER事件进行分组。然后结合它们的步骤,这样我就可以返回这样的东西。
RECEIVER_NAME
MIN-RECEIVER_START_DATETIME
MAX-RECEIVER_END_DATETIME
MIN-SOURCE_END_DATETIME
MAX-SOURCE_END_DATETIME
LIST_OF_SOURCES < - 从Allen Browne's ConcatRelated() function生成的字段“SOURCE10,SOURCE 24,SOURCE 51”。
我认为我理解这种方法,但是我无法正确分组有超过2个触发器的东西。如果我得到适当的时间分组,我可以解决使用ConcatRelated连接源名称的问题。
- 更新 - 我已将一些示例数据上传到SQLfiddle.com click here for sample dataset
我基本上想要提出的结果表对于此示例数据集看起来像这样:
RECEIVER_NAME MIN-RECEIVER_START_DATETIME MAX-RECEIVER_END_DATETIME SOURCE_LIST MIN-SOURCE_START_DATETIME MAX-SOURCE_END_DATETIME RECEIVER1 2012-04-08 05:08 2012-04-08 06:22 SOURCE1,SOURCE2,SOURCE3 2012-04-08 02:10 2012-04-08 05:25 RECEIVER2 2012-05-08 10:05 2012-04-08 14:55 SOURCE1,SOURCE2 2012-05-08 10:01 2012-05-08 13:45 RECEIVER2 2012-06-08 06:55 2012-06-08 21:19 SOURCE2 2012-05-08 14:55 2012-05-08 16:22抱歉,哇试图张贴一张桌子真是太痛苦了。我找不到更好的方法。
答案 0 :(得分:0)
正如我在评论中提到的没有标准已用于产生结果。您的活动按RECEIVER_EVENT_ID和RECEIVER_EVENT_START_TIME分组。 (我猜receiver_event_id总是与receiver_name相关,因此我选择了event_id,但你也可以通过receiver_name对它进行分组)
这将暂时给出:
然后您可以找到与正在分组的事件相关的最小值和最大值。 如果您想将事件分组241& 242,你需要找到一个逻辑,将它们组合在一起。
这是分组事件和事件开始时间的代码降价:
希望这能让您了解MySQL中的group_concatenation函数以及分组。如果您已找到针对您的确切SQL语句或更快的解决方案,请告诉我们。我也非常有兴趣看到它。
MySQL 5.5.32架构设置:
CREATE TABLE relatedEvents
(
CORRELATION_ID INT auto_increment primary key,
RECEIVER_EVENT_ID INT,
RECEIVER_NAME VARCHAR(20),
RECEIVER_START_DATETIME DATETIME,
RECEIVER_END_DATETIME DATETIME,
SOURCE_EVENT_ID INT,
SOURCE_NAME VARCHAR(20),
SOURCE_START_DATETIME DATETIME,
SOURCE_END_DATETIME DATETIME
);
INSERT INTO relatedEvents
(RECEIVER_EVENT_ID, RECEIVER_NAME, RECEIVER_START_DATETIME,
RECEIVER_END_DATETIME, SOURCE_EVENT_ID, SOURCE_NAME, SOURCE_START_DATETIME, SOURCE_END_DATETIME)
VALUES
('240', 'RECEIVER1', '2012-08-04 05:08:00', '2012-08-04 06:22', '1', 'SOURCE1', '2012-08-04 02:10', '2012-08-04 02:40'),
('240', 'RECEIVER1', '2012-08-04 05:08:00', '2012-08-04 06:22', '2', 'SOURCE2', '2012-08-04 02:30', '2012-08-04 03:10'),
('240', 'RECEIVER1', '2012-08-04 05:08:00', '2012-08-04 06:22', '3', 'SOURCE2', '2012-08-04 03:15', '2012-08-04 03:30'),
('240', 'RECEIVER1', '2012-08-04 05:08:00', '2012-08-04 06:22', '4', 'SOURCE3', '2012-08-04 05:01', '2012-08-04 05:25'),
('241', 'RECEIVER2', '2012-08-05 10:05:00', '2012-08-05 10:35', '5', 'SOURCE1', '2012-08-05 10:01', '2012-08-05 10:15'),
('241', 'RECEIVER2', '2012-08-05 10:05:00', '2012-08-05 10:35', '6', 'SOURCE2', '2012-08-05 12:15', '2012-08-05 12:17'),
('242', 'RECEIVER2', '2012-08-05 14:15:00', '2012-08-05 14:55', '7', 'SOURCE1', '2012-08-05 13:35', '2012-08-05 13:45'),
('243', 'RECEIVER2', '2012-08-06 06:55:00', '2012-08-06 21:19', '8', 'SOURCE2', '2012-08-05 14:55', '2012-08-05 16:22');
查询1 :
SELECT
RECEIVER_EVENT_ID as EVENT_ID,
o_r.receiver_name as Receiver_name,
(select min(RECEIVER_START_DATETIME)from relatedEvents as I_R where ((I_R.RECEIVER_EVENT_ID = O_R.RECEIVER_EVENT_ID)) Group by I_R.RECEIVER_EVENT_ID)as min_r_st,
(select Max(RECEIVER_END_DATETIME)from relatedEvents as I_R where ((I_R.RECEIVER_EVENT_ID = O_R.RECEIVER_EVENT_ID)) Group by I_R.RECEIVER_EVENT_ID) as max_r_et,
(Select group_concat(DISTINCT source_name) from relatedEvents as I_R where ((I_R.RECEIVER_EVENT_ID = O_R.RECEIVER_EVENT_ID)) Group by I_R.RECEIVER_EVENT_ID) as Sources,
(select min(SOURCE_START_DATETIME)from relatedEvents as I_R where ((I_R.RECEIVER_EVENT_ID = O_R.RECEIVER_EVENT_ID)) Group by I_R.RECEIVER_EVENT_ID)as min_s_st,
(select Max(SOURCE_END_DATETIME)from relatedEvents as I_R where ((I_R.RECEIVER_EVENT_ID = O_R.RECEIVER_EVENT_ID)) Group by I_R.RECEIVER_EVENT_ID) as max_s_et,
count(RECEIVER_START_DATETIME) as RST
FROM relatedEvents as O_R
group by RECEIVER_EVENT_ID, RECEIVER_START_DATETIME
order by RECEIVER_START_DATETIME asc
<强> Results 强>:
| EVENT_ID | RECEIVER_NAME | MIN_R_ST | MAX_R_ET | SOURCES | MIN_S_ST | MAX_S_ET | RST |
|----------|---------------|-------------------------------|-------------------------------|-------------------------|-------------------------------|-------------------------------|-----|
| 240 | RECEIVER1 | August, 04 2012 05:08:00+0000 | August, 04 2012 06:22:00+0000 | SOURCE1,SOURCE2,SOURCE3 | August, 04 2012 02:10:00+0000 | August, 04 2012 05:25:00+0000 | 4 |
| 241 | RECEIVER2 | August, 05 2012 10:05:00+0000 | August, 05 2012 10:35:00+0000 | SOURCE1,SOURCE2 | August, 05 2012 10:01:00+0000 | August, 05 2012 12:17:00+0000 | 2 |
| 242 | RECEIVER2 | August, 05 2012 14:15:00+0000 | August, 05 2012 14:55:00+0000 | SOURCE1 | August, 05 2012 13:35:00+0000 | August, 05 2012 13:45:00+0000 | 1 |
| 243 | RECEIVER2 | August, 06 2012 06:55:00+0000 | August, 06 2012 21:19:00+0000 | SOURCE2 | August, 05 2012 14:55:00+0000 | August, 05 2012 16:22:00+0000 | 1 |