English 中文(简体)
表格中哪一行(按一栏排列)与另一个表格中的一行相对应?
原标题:Which row (in order by some column) in a table corresponds to a row in another table?

我有以下两个表格(从实际中简单化):

mysql> desc small_table;
+-----------------+---------------+------+-----+---------+-------+
| Field           | Type          | Null | Key | Default | Extra |
+-----------------+---------------+------+-----+---------+-------+
| event_time      | datetime      | NO   |     | NULL    |       |
| user_id         | char(15)      | NO   |     | NULL    |       |
| other_data      | int(11)       | NO   | MUL | NULL    |       |
+-----------------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> desc large_table;
+-----------------+---------------+------+-----+---------+-------+
| Field           | Type          | Null | Key | Default | Extra |
+-----------------+---------------+------+-----+---------+-------+
| event_time      | datetime      | NO   |     | NULL    |       |
| user_id         | char(15)      | NO   |     | NULL    |       |
| other_data      | int(11)       | NO   |     | NULL    |       |
+-----------------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

现在,small_table 是小的:每张user_id 通常只有一行(但有时会更多)。 而在<代码>large_table 上,则每一条<代码>用户_id 似乎有无数次。

mysql> select count(1) from small_tableG
*************************** 1. row ***************************
count(1): 20182
1 row in set (0.00 sec)


mysql> select count(1) from large_tableG
*************************** 1. row ***************************
count(1): 2870522
1 row in set (0.00 sec)

然而,对于<代码>small_table上的每行,在<代码>large_table上至少有一行,其编号为user_id、同一<代码>其他_data和类似_time。 (在几分钟内也是如此)。

我想知道small_table是否有与第一或第二行对应的行号,或large_table中为同一条<代码>用户_id和类似的event_time上的不同行号。 这就是说:

  1. for each user_id, a count of distinct rows of large_table in order by event_time, but only for event_time within, say, three hours; that is, I seek only the count of such rows as have event_time within, say, three hours of one another; and
  2. for each such collection of distinct rows, an identification of which row in that list (in order by event_time) has a corresponding row in small_table.

我甚至似乎也能够写出第一步的问话,更不用说将做第二步的问话,并且会欣赏任何方向。

问题回答

这样做是必要的残酷的;它将使你的优化工作真正相当严肃。

根据问题之后的评论和问题,如果某一用户的身份证在邻近事件之间有固定间隔,则希望在大张桌上将事件顺序作为毗连。 例如,固定间隔为3小时。 为IBM Informix Dynamic服务器提供的I m编码(为论证,第11.70版,但还有11.50页。 这意味着,我需要解释一下的是非同义词。 具体来说,编号3 UNITS HOUR是指3小时的间隔;也可在标准Q中写成:INTERVAL(3) HOUR

在制作SQ,特别是SQL综合体方面有两种关键技术。 其中一个步骤是把最后结果编成零敲碎打。 另一种做法是,确保你清楚地知道你之后的情况。

在下面的注解中,对同一用户(ID)的资格应当被视为始终属于该表述的一部分。

In the large table, there are three categories of range that we want to consider before joining with the small table.

  1. Entries where there is neither a row with an event time before the event that is close enough nor a row with an event time after the event that is close enough. This is a time range with the same start and end time.
  2. A pair of entries in the table that are themselves close enough but for which there is neither an event earlier than the early event of the pair that is close enough nor an event later than the late event of the pair that is close enough nor an event in between the pair.
  3. A sequence of three or more entries in the table for which there is:
    • No event E1 earlier than the earliest that is close enough
    • No event E2 later than the latest that is close enough
    • An event E3 later than the earliest that is close enough to the earliest
    • An event E4 earlier than the latest that is close enough to the latest (E4 might possibly be the same event as E3)
    • No pair of events E5, E6 between the earliest and the latest where there is no event in between E5 and E6 but the gap between E5 and E6 is too large to count.

你们可以从描述中看到,这将是一些ary!

NB:现在对该法典进行了测试;一些(主要是小的)改动是必要的。 一项不必要的小改动是,根据关于中间询问的条款增加ER。 另一项不必要的改动是,为核查目的从小表格中挑选其他数据。 这一修订是在没有研究msh210>的修改文本的情况下进行的。

Also note that I m far from certain this is a minimal formulation; it may be feasible to classify all the ranges with a single SELECT statement instead of a UNION of three SELECT statements (and it would be good if that is the case).

Singleton ranges

-- Ranges of exactly 1 event
SELECT lt1.user_id, lt1.event_time AS min_time, lt1.event_time AS max_time
  FROM Large_Table AS lt1
 WHERE NOT EXISTS -- an earlier event that is close enough
       (SELECT *
          FROM Large_Table AS lt3
         WHERE lt1.user_id = lt3.user_id
           AND lt3.event_time > lt1.event_time - 3 UNITS HOUR
           AND lt3.event_time < lt1.event_time
       )
   AND NOT EXISTS -- a later event that is close enough
       (SELECT *
          FROM Large_Table AS lt4
         WHERE lt1.user_id = lt4.user_id
           AND lt4.event_time > lt1.event_time
           AND lt4.event_time < lt1.event_time + 3 UNITS HOUR
       )
ORDER BY User_ID, Min_Time;

Doubleton Ranges

-- Ranges of exactly 2 events
SELECT lt1.user_id, lt1.event_time AS min_time, lt2.event_time AS max_time
  FROM Large_Table AS lt1
  JOIN Large_Table AS lt2
    ON lt1.user_id = lt2.user_id
   AND lt1.event_time < lt2.event_time
   AND lt2.event_time < lt1.event_time + 3 UNITS HOUR
 WHERE NOT EXISTS -- an earlier event that is close enough
       (SELECT *
          FROM Large_Table AS lt3
         WHERE lt1.user_id = lt3.user_id
           AND lt3.event_time > lt1.event_time - 3 UNITS HOUR
           AND lt3.event_time < lt1.event_time
       )
   AND NOT EXISTS -- a later event that is close enough
       (SELECT *
          FROM Large_Table AS lt4
         WHERE lt1.user_id = lt4.user_id
           AND lt4.event_time > lt2.event_time
           AND lt4.event_time < lt2.event_time + 3 UNITS HOUR
       )
   AND NOT EXISTS -- any event in between
       (SELECT *
          FROM Large_Table AS lt5
         WHERE lt1.user_id = lt5.user_id
           AND lt5.event_time > lt1.event_time
           AND lt5.event_time < lt2.event_time
       )
ORDER BY User_ID, Min_Time;

http://www.un.org/Depts/DGACM/index_french.htm

Multiple Event Ranges

-- Ranges of 3 or more events
SELECT lt1.user_id, lt1.event_time AS min_time, lt2.event_time AS max_time
  FROM Large_Table AS lt1
  JOIN Large_Table AS lt2
    ON lt1.user_id = lt2.user_id
   AND lt1.event_time < lt2.event_time
 WHERE NOT EXISTS -- an earlier event that is close enough
       (SELECT *
          FROM Large_Table AS lt3
         WHERE lt1.user_id = lt3.user_id
           AND lt3.event_time > lt1.event_time - 3 UNITS HOUR
           AND lt3.event_time < lt1.event_time
       )
   AND NOT EXISTS -- a later event that is close enough
       (SELECT *
          FROM Large_Table AS lt4
         WHERE lt1.user_id = lt4.user_id
           AND lt4.event_time > lt2.event_time
           AND lt4.event_time < lt2.event_time + 3 UNITS HOUR
       )
   AND NOT EXISTS -- a gap that s too big in the events between first and last
       (SELECT *
          FROM Large_Table AS lt5 -- E5 before E6
          JOIN Large_Table AS lt6
            ON lt5.user_id = lt6.user_id
           AND lt5.event_time < lt6.event_time
         WHERE lt1.user_id = lt5.user_id
           AND lt6.event_time < lt2.event_time
           AND lt5.event_time > lt1.event_time
           AND (lt6.event_time - lt5.event_time) > 3 UNITS HOUR
           AND NOT EXISTS -- an event in between these two
               (SELECT *
                  FROM Large_Table AS lt9
                 WHERE lt5.user_id = lt9.user_id
                   AND lt9.event_time > lt5.event_time
                   AND lt9.event_time < lt6.event_time
               )
       )
   AND EXISTS -- an event close enough after the start
       (SELECT *
          FROM Large_Table AS lt7
         WHERE lt1.user_id = lt7.user_id
           AND lt1.event_time < lt7.event_time
           AND lt7.event_time < lt1.event_time + 3 UNITS HOUR
           AND lt7.event_time < lt2.event_time
       )
   AND EXISTS -- an event close enough before the end
       (SELECT *
          FROM Large_Table AS lt8
         WHERE lt2.user_id = lt8.user_id
           AND lt2.event_time > lt8.event_time
           AND lt8.event_time > lt2.event_time - 3 UNITS HOUR
           AND lt8.event_time > lt1.event_time
       )
ORDER BY User_ID, Min_Time;

Added 漏掉了在大差距分局中设置的NOT EXISTS条款。

All Ranges in Large Table

Clearly, the complete list of ranges in the last table are the union of the three queries above.

Query deleted as not interesting enough. It is simply the 3-way UNION of the separate queries above.

Final Query

最后的询问发现,如果存在任何范围,则会由于三个部分的贪.,而这种划分接近于小表的输入。 小型表格的单一条目可能下降为1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1。 从大型桌子的两种幅度是分开的(两者之间的差距是4小时),但小桌子的条目接近两倍。 [ 测试涵盖本案:

SELECT S.User_id, S.Event_Time, L.Min_Time, L.Max_Time, S.Other_Data
  FROM Small_Table AS S
  JOIN (
        -- Ranges of exactly 1 event
        SELECT lt1.user_id, lt1.event_time AS min_time, lt1.event_time AS max_time
          FROM Large_Table AS lt1
         WHERE NOT EXISTS -- an earlier event that is close enough
               (SELECT *
                  FROM Large_Table AS lt3
                 WHERE lt1.user_id = lt3.user_id
                   AND lt3.event_time > lt1.event_time - 3 UNITS HOUR
                   AND lt3.event_time < lt1.event_time
               )
           AND NOT EXISTS -- a later event that is close enough
               (SELECT *
                  FROM Large_Table AS lt4
                 WHERE lt1.user_id = lt4.user_id
                   AND lt4.event_time > lt1.event_time
                   AND lt4.event_time < lt1.event_time + 3 UNITS HOUR
               )
        UNION
        -- Ranges of exactly 2 events
        SELECT lt1.user_id, lt1.event_time AS min_time, lt2.event_time AS max_time
          FROM Large_Table AS lt1
          JOIN Large_Table AS lt2
            ON lt1.user_id = lt2.user_id
           AND lt1.event_time < lt2.event_time
           AND lt2.event_time < lt1.event_time + 3 UNITS HOUR
         WHERE NOT EXISTS -- an earlier event that is close enough
               (SELECT *
                  FROM Large_Table AS lt3
                 WHERE lt1.user_id = lt3.user_id
                   AND lt3.event_time > lt1.event_time - 3 UNITS HOUR
                   AND lt3.event_time < lt1.event_time
               )
           AND NOT EXISTS -- a later event that is close enough
               (SELECT *
                  FROM Large_Table AS lt4
                 WHERE lt1.user_id = lt4.user_id
                   AND lt4.event_time > lt2.event_time
                   AND lt4.event_time < lt2.event_time + 3 UNITS HOUR
               )
           AND NOT EXISTS -- any event in between
               (SELECT *
                  FROM Large_Table AS lt5
                 WHERE lt1.user_id = lt5.user_id
                   AND lt5.event_time > lt1.event_time
                   AND lt5.event_time < lt2.event_time
               )
        UNION
        -- Ranges of 3 or more events
        SELECT lt1.user_id, lt1.event_time AS min_time, lt2.event_time AS max_time
          FROM Large_Table AS lt1
          JOIN Large_Table AS lt2
            ON lt1.user_id = lt2.user_id
           AND lt1.event_time < lt2.event_time
         WHERE NOT EXISTS -- an earlier event that is close enough
               (SELECT *
                  FROM Large_Table AS lt3
                 WHERE lt1.user_id = lt3.user_id
                   AND lt3.event_time > lt1.event_time - 3 UNITS HOUR
                   AND lt3.event_time < lt1.event_time
               )
           AND NOT EXISTS -- a later event that is close enough
               (SELECT *
                  FROM Large_Table AS lt4
                 WHERE lt1.user_id = lt4.user_id
                   AND lt4.event_time > lt2.event_time
                   AND lt4.event_time < lt2.event_time + 3 UNITS HOUR
               )
           AND NOT EXISTS -- a gap that s too big in the events between first and last
               (SELECT *
                  FROM Large_Table AS lt5 -- E5 before E6
                  JOIN Large_Table AS lt6
                    ON lt5.user_id = lt6.user_id
                   AND lt5.event_time < lt6.event_time
                 WHERE lt1.user_id = lt5.user_id
                   AND lt6.event_time < lt2.event_time
                   AND lt5.event_time > lt1.event_time
                   AND (lt6.event_time - lt5.event_time) > 3 UNITS HOUR
                   AND NOT EXISTS -- an event in between these two
                       (SELECT *
                          FROM Large_Table AS lt9
                         WHERE lt5.user_id = lt9.user_id
                           AND lt9.event_time > lt5.event_time
                           AND lt9.event_time < lt6.event_time
                       )
               )
           AND EXISTS -- an event close enough after the start
               (SELECT *
                  FROM Large_Table AS lt7
                 WHERE lt1.user_id = lt7.user_id
                   AND lt1.event_time < lt7.event_time
                   AND lt7.event_time < lt1.event_time + 3 UNITS HOUR
                   AND lt7.event_time < lt2.event_time
               )
           AND EXISTS -- an event close enough before the end
               (SELECT *
                  FROM Large_Table AS lt8
                 WHERE lt2.user_id = lt8.user_id
                   AND lt2.event_time > lt8.event_time
                   AND lt8.event_time > lt2.event_time - 3 UNITS HOUR
                   AND lt8.event_time > lt1.event_time
               )
       ) AS L
    ON S.User_ID = L.User_ID
 WHERE S.Event_Time > L.Min_Time - 3 UNITS HOUR
   AND S.Event_Time < L.Max_Time + 3 UNITS HOUR
ORDER BY User_ID, Event_Time, Min_Time;

OK——公平的警告;LQ没有在房舍管理处附近任何地点。

现在对该代码进行了测试。 无限机会实际上为零;存在一对一的错误,并存在一个或多个次要问题。

我在设计测试数据后进行了试验。 我使用了阿尔法数据(见下文),同时验证和确定询问,并增加了Beta数据,以确保不同用户——ID值之间没有交叉数字。

我使用了明确的<代码><和> 业务而不是。 BETWEEN 和,以排除最终点;如果你想把事情完全分开3小时,以便算得足够接近,那么你需要审查每一不平等情况,可能将其改为。 BETWEEN 和 或可酌情使用<代码>>=或<=

answer 对于一个大致相似但较为简单的问题,即:(a) 我写了字,(b) 就上述复杂处理提供了一些有益的想法(特别是,以前没有发生但过得足够、但后来又没有结束的事件)。 足够严格的标准无疑使这一问题复杂化。


Test Data

Large Table

CREATE TABLE Large_Table
(
    Event_Time  DATETIME YEAR TO MINUTE NOT NULL,
    User_ID     CHAR(15) NOT NULL,
    Other_Data  INTEGER NOT NULL,
    PRIMARY KEY(User_ID, Event_Time)
);

INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 09:15 ,  Alpha ,  1) { R4 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 11:15 ,  Alpha ,  2) { R4 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 13:15 ,  Alpha ,  3) { R4 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 15:15 ,  Alpha ,  4) { R4 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 12:17 ,  Beta ,   1) { R4 };

INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-02 09:15 ,  Alpha ,  5) { R1 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-02 10:17 ,  Beta ,   2) { R1 };

INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-03 09:15 ,  Alpha ,  6) { R2 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-03 11:15 ,  Alpha ,  7) { R2 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-03 10:17 ,  Beta ,   3) { R1 };

INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-04 09:15 ,  Alpha ,  8) { R3 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-04 11:15 ,  Alpha ,  9) { R3 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-04 13:15 ,  Alpha , 10) { R3 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-04 10:17 ,  Beta ,   4) { R1 };

INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 09:15 ,  Alpha , 11) { R2 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 11:15 ,  Alpha , 12) { R2 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 10:17 ,  Beta ,   5) { R1 };
{ Probe here }
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 15:15 ,  Alpha , 13) { R2 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 17:15 ,  Alpha , 14) { R2 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 16:17 ,  Beta ,   6) { R1 };

INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 09:15 ,  Alpha , 15) { R6 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 11:15 ,  Alpha , 16) { R6 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 13:15 ,  Alpha , 17) { R6 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 15:15 ,  Alpha , 18) { R6 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 17:15 ,  Alpha , 19) { R6 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 19:15 ,  Alpha , 20) { R6 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 16:17 ,  Beta ,   7) { R1 };

INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-07 09:15 ,  Alpha , 21) { R1 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-07 11:17 ,  Beta ,   8) { R1 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-07 12:15 ,  Alpha , 22) { R1 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-07 13:17 ,  Beta ,   9) { R1 };

INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-08 09:15 ,  Alpha , 23) { R5 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-08 11:15 ,  Alpha , 24) { R5 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-08 13:15 ,  Alpha , 25) { R5 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-08 15:15 ,  Alpha , 26) { R5 };
INSERT INTO Large_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-08 17:15 ,  Alpha , 27) { R5 };

Small Table

注:为测试目的,小表实际上包含的浏览量比大。 小型表格中与100多个其他元数值的行文不应出现在结果中(也不应出现)。 这里的测试是在边缘条件下进行的。

CREATE TABLE Small_Table
(
    Event_Time  DATETIME YEAR TO MINUTE NOT NULL,
    User_ID     CHAR(15) NOT NULL,
    Other_Data  INTEGER NOT NULL,
    PRIMARY KEY(User_ID, Event_Time)
);

INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 06:15 ,  Alpha , 131) { XX };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 06:20 ,  Alpha ,  31) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 10:20 ,  Alpha ,  32) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 13:20 ,  Alpha ,  33) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 15:20 ,  Alpha ,  34) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 18:15 ,  Alpha , 134) { XX };

INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-02 06:15 ,  Alpha , 135) { XX };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-02 06:16 ,  Alpha ,  35) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-02 10:20 ,  Alpha ,  35) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-02 12:14 ,  Alpha ,  35) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-02 12:15 ,  Alpha , 135) { XX };

INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-03 09:20 ,  Alpha ,  36) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-03 11:20 ,  Alpha ,  37) { YY };

INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-04 09:20 ,  Alpha ,  38) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-04 11:20 ,  Alpha ,  39) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-04 13:20 ,  Alpha ,  40) { YY };

INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 09:20 ,  Alpha ,  41) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 11:20 ,  Alpha ,  42) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 13:20 ,  Alpha ,  42) { 22 };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 15:20 ,  Alpha ,  43) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 17:20 ,  Alpha ,  44) { YY };

INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 09:20 ,  Alpha ,  45) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 11:20 ,  Alpha ,  46) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 13:20 ,  Alpha ,  47) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 15:20 ,  Alpha ,  48) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 17:20 ,  Alpha ,  49) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 19:20 ,  Alpha ,  50) { YY };

INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-07 09:20 ,  Alpha ,  51) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-07 10:20 ,  Alpha ,  51) { 22 };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-07 12:20 ,  Alpha ,  52) { YY };

INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-08 09:20 ,  Alpha ,  53) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-08 11:20 ,  Alpha ,  54) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-08 13:20 ,  Alpha ,  55) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-08 15:20 ,  Alpha ,  56) { YY };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-08 17:20 ,  Alpha ,  57) { YY };


INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-07 13:27 ,  Beta ,   9) { R1 };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-07 11:27 ,  Beta ,   8) { R1 };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-06 16:27 ,  Beta ,   7) { R1 };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 16:27 ,  Beta ,   6) { R1 };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-05 10:27 ,  Beta ,   5) { R1 };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-04 10:27 ,  Beta ,   4) { R1 };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-03 10:27 ,  Beta ,   3) { R1 };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-02 10:27 ,  Beta ,   2) { R1 };
INSERT INTO Small_Table(Event_Time, User_ID, Other_Data) VALUES( 2012-01-01 12:27 ,  Beta ,   1) { R4 };

Final Query Results

根据上述数据,取得的成果是:

Alpha   2012-01-01 06:20   2012-01-01 09:15   2012-01-01 15:15   31
Alpha   2012-01-01 10:20   2012-01-01 09:15   2012-01-01 15:15   32
Alpha   2012-01-01 13:20   2012-01-01 09:15   2012-01-01 15:15   33
Alpha   2012-01-01 15:20   2012-01-01 09:15   2012-01-01 15:15   34
Alpha   2012-01-02 06:16   2012-01-02 09:15   2012-01-02 09:15   35
Alpha   2012-01-02 10:20   2012-01-02 09:15   2012-01-02 09:15   35
Alpha   2012-01-02 12:14   2012-01-02 09:15   2012-01-02 09:15   35
Alpha   2012-01-03 09:20   2012-01-03 09:15   2012-01-03 11:15   36
Alpha   2012-01-03 11:20   2012-01-03 09:15   2012-01-03 11:15   37
Alpha   2012-01-04 09:20   2012-01-04 09:15   2012-01-04 13:15   38
Alpha   2012-01-04 11:20   2012-01-04 09:15   2012-01-04 13:15   39
Alpha   2012-01-04 13:20   2012-01-04 09:15   2012-01-04 13:15   40
Alpha   2012-01-05 09:20   2012-01-05 09:15   2012-01-05 11:15   41
Alpha   2012-01-05 11:20   2012-01-05 09:15   2012-01-05 11:15   42
Alpha   2012-01-05 13:20   2012-01-05 09:15   2012-01-05 11:15   42
Alpha   2012-01-05 13:20   2012-01-05 15:15   2012-01-05 17:15   42
Alpha   2012-01-05 15:20   2012-01-05 15:15   2012-01-05 17:15   43
Alpha   2012-01-05 17:20   2012-01-05 15:15   2012-01-05 17:15   44
Alpha   2012-01-06 09:20   2012-01-06 09:15   2012-01-06 19:15   45
Alpha   2012-01-06 11:20   2012-01-06 09:15   2012-01-06 19:15   46
Alpha   2012-01-06 13:20   2012-01-06 09:15   2012-01-06 19:15   47
Alpha   2012-01-06 15:20   2012-01-06 09:15   2012-01-06 19:15   48
Alpha   2012-01-06 17:20   2012-01-06 09:15   2012-01-06 19:15   49
Alpha   2012-01-06 19:20   2012-01-06 09:15   2012-01-06 19:15   50
Alpha   2012-01-07 09:20   2012-01-07 09:15   2012-01-07 09:15   51
Alpha   2012-01-07 09:20   2012-01-07 12:15   2012-01-07 12:15   51
Alpha   2012-01-07 10:20   2012-01-07 09:15   2012-01-07 09:15   51
Alpha   2012-01-07 10:20   2012-01-07 12:15   2012-01-07 12:15   51
Alpha   2012-01-07 12:20   2012-01-07 12:15   2012-01-07 12:15   52
Alpha   2012-01-08 09:20   2012-01-08 09:15   2012-01-08 17:15   53
Alpha   2012-01-08 11:20   2012-01-08 09:15   2012-01-08 17:15   54
Alpha   2012-01-08 13:20   2012-01-08 09:15   2012-01-08 17:15   55
Alpha   2012-01-08 15:20   2012-01-08 09:15   2012-01-08 17:15   56
Alpha   2012-01-08 17:20   2012-01-08 09:15   2012-01-08 17:15   57
Beta    2012-01-01 12:27   2012-01-01 12:17   2012-01-01 12:17    1
Beta    2012-01-02 10:27   2012-01-02 10:17   2012-01-02 10:17    2
Beta    2012-01-03 10:27   2012-01-03 10:17   2012-01-03 10:17    3
Beta    2012-01-04 10:27   2012-01-04 10:17   2012-01-04 10:17    4
Beta    2012-01-05 10:27   2012-01-05 10:17   2012-01-05 10:17    5
Beta    2012-01-05 16:27   2012-01-05 16:17   2012-01-05 16:17    6
Beta    2012-01-06 16:27   2012-01-06 16:17   2012-01-06 16:17    7
Beta    2012-01-07 11:27   2012-01-07 11:17   2012-01-07 13:17    8
Beta    2012-01-07 13:27   2012-01-07 11:17   2012-01-07 13:17    9

Intermediate results

格式差别很大。

Singleton ranges

Alpha|2012-01-02 09:15|2012-01-02 09:15
Alpha|2012-01-07 09:15|2012-01-07 09:15
Alpha|2012-01-07 12:15|2012-01-07 12:15
Beta|2012-01-01 12:17|2012-01-01 12:17
Beta|2012-01-02 10:17|2012-01-02 10:17
Beta|2012-01-03 10:17|2012-01-03 10:17
Beta|2012-01-04 10:17|2012-01-04 10:17
Beta|2012-01-05 10:17|2012-01-05 10:17
Beta|2012-01-05 16:17|2012-01-05 16:17
Beta|2012-01-06 16:17|2012-01-06 16:17

2. 双重吨位

Alpha|2012-01-03 09:15|2012-01-03 11:15
Alpha|2012-01-05 09:15|2012-01-05 11:15
Alpha|2012-01-05 15:15|2012-01-05 17:15
Beta|2012-01-07 11:17|2012-01-07 13:17

多个活动领域

Alpha|2012-01-01 09:15|2012-01-01 15:15
Alpha|2012-01-04 09:15|2012-01-04 13:15
Alpha|2012-01-06 09:15|2012-01-06 19:15
Alpha|2012-01-08 09:15|2012-01-08 17:15
select count(s.user_id), s.event_time, s.other_data from small_table s
where s.user_id IN (select distinct user_id from big_table where event_time between  StartDate  and  EndDate )
order by s.event_time

我不肯定你在你提到的小幅度内要求做些什么。

并且:

select * from large_table t1, large_table t2 
where t1.event_time <= date_sub(t2.event_time, INTERVAL 3 hour)

因此,尝试:

  select count(s.user_id), s.event_time, s.other_data from small_table s
    where s.user_id IN ( select * from large_table t1, large_table t2 
    where t1.event_time <= date_sub(t2.event_time, INTERVAL 3 hour))
order by s.event_time

这也许是关于

在Jonathan Leffler的答复中,题为“大事范围”的法典认为,在第一次审理后不久,二审即告结束,最后没有出现大的中断,但禁止内部情况之间出现任何巨大差距,即使两者有其他情况。 因此,例如,如果限制为3小时,则由于2至6之间的差距,将禁止1、2、4、6和7个限制。 我认为,正确的法典是(直接建立在Jonathan Leffler的基础之上):

SELECT lt1.user_id, lt1.event_time AS min_time, lt2.event_time AS max_time
  FROM Large_Table AS lt1
  JOIN Large_Table AS lt2
    ON lt1.user_id = lt2.user_id
   AND lt1.event_time < lt2.event_time
 WHERE NOT EXISTS -- an earlier event that is close enough
       (SELECT *
          FROM Large_Table AS lt3
         WHERE lt1.user_id = lt3.user_id
           AND lt3.event_time > lt1.event_time - 3 UNITS HOUR
           AND lt3.event_time < lt1.event_time
       )
   AND NOT EXISTS -- a later event that is close enough
       (SELECT *
          FROM Large_Table AS lt4
         WHERE lt1.user_id = lt4.user_id
           AND lt4.event_time > lt2.event_time
           AND lt4.event_time < lt2.event_time + 3 UNITS HOUR
       )
   AND NOT EXISTS -- a gap that s too big in the events between first and last
       (SELECT *
          FROM Large_Table AS lt5 -- E5 before E6
          JOIN Large_Table AS lt6
            ON lt5.user_id = lt6.user_id
           AND lt1.user_id = lt5.user_id
           AND lt5.event_time < lt6.event_time
           AND lt6.event_time <= lt2.event_time
           AND lt5.event_time >= lt1.event_time
           AND (lt6.event_time - lt5.event_time) > 3 UNITS HOUR
           and not exists (
             select * from large_table as lt9 
               where lt9.event_time > lt5.event_time
                 and lt6.event_time > lt9.event_time
             )
       )

在Jonathan Leffler的答复中,删除了最后两条<代码>和的需要,并在回答中删除了“Singleton幅度”和“Doubleton幅度”代码的必要性。

Unless I m missing something.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...