7
+----------+--------------+-------------------------+
| ticketid | ticketpostid |           date          |
+----------+--------------+-------------------------+
|  1387935 |      3147808 | 2012-09-17 13:33:01     |
|  1387935 |      3147812 | 2012-09-17 13:33:41     |
|  1387938 |      3147818 | 2012-09-17 13:35:01     |
|  1387938 |      3148068 | 2012-09-17 13:37:01     |
|  1387938 |      3148323 | 2012-09-17 14:47:01     |
|  1387939 |      3147820 | 2012-09-17 13:36:01     |
|  1387939 |      3147834 | 2012-09-17 13:36:25     |
|  1387939 |      3147851 | 2012-09-17 13:41:01     |
|  1387939 |      3147968 | 2012-09-17 13:59:06     |
|  1387939 |      3147996 | 2012-09-17 14:03:01     |

这是我写的查询的结果。有两行和两行以上具有相同的ticketid。我需要在每个ticketid中找到前两个日期之间的时差

前任。

+----------+--------------+-------------------------+
| ticketid | ticketpostid |           date          |
+----------+--------------+-------------------------+
|  1387935 |      3147808 | 2012-09-17 13:33:01     |
|  1387935 |      3147812 | 2012-09-17 13:33:41     |
|  1387938 |      3147818 | 2012-09-17 13:35:01     |
|  1387938 |      3148068 | 2012-09-17 13:37:01     |
|  1387939 |      3147820 | 2012-09-17 13:36:01     |
|  1387939 |      3147834 | 2012-09-17 13:36:25     |

因此;

+----------+--------------+
| ticketid |time diff(sec)|
+----------+--------------+
|  1387935 |      40      |
|  1387938 |      120     |
|  1387939 |      24      |

你能告诉我我该怎么做吗?

谢谢。

4

3 回答 3

14

对于 PostgreSQL,我认为您希望lag窗口函数来比较行;它会比自加入和过滤器更有效。这不适用于 MySQL,因为它似乎仍然不支持标准 SQL:2003 窗口函数;见下文。

要仅找到两个最低的,您可以在 上使用dense_rank窗口函数ticketid,然后过滤结果以仅返回 where 的行dense_rank() = 2,即具有倒数第二个时间戳的行,其中lag()将产生具有最低时间戳的行。

请参阅此 SQLFiddle ,它显示了示例 DDL 和输出。

SELECT ticketid, extract(epoch from tdiff) FROM (
  SELECT
      ticketid,
      ticketdate - lag(ticketdate) OVER (PARTITION BY ticketid ORDER BY ticketdate) AS tdiff,
      dense_rank() OVER (PARTITION BY ticketid ORDER BY ticketdate) AS rank
  FROM Table1
  ORDER BY ticketid) x
WHERE rank = 2;

我已将其用作ticketdate日期列的名称,因为date它是一个糟糕的列名称(它是一个数据类型名称)并且永远不应该使用;在许多情况下必须双引号才能工作。

便携式方法可能是其他人发布的自我加入。上面的窗口函数方法可能也适用于 Oracle,但似乎不适用于 MySQL。据我所知,它不支持 SQL:2003 窗口函数。

如果您SET sql_mode = 'ANSI'使用timestamp而不是timestamp with time zone. 似乎窗口函数不会;MySQL 在该OVER子句上窒息。请参阅此 SQLFiddle

于 2012-09-17T12:14:07.847 回答
2

试试这个查询 -

INSERT INTO ticket_post(ticketid, ticketpostid, date) VALUES
(1387935, 3147808, '2012-09-17 13:33:01'),
(1387935, 3147812, '2012-09-17 13:33:41'),
(1387938, 3147818, '2012-09-17 13:35:01'),
(1387938, 3148068, '2012-09-17 13:37:01'),
(1387938, 3148323, '2012-09-17 14:47:01'),
(1387939, 3147820, '2012-09-17 13:36:01'),
(1387939, 3147834, '2012-09-17 13:36:25'),
(1387939, 3147851, '2012-09-17 13:41:01'),
(1387939, 3147968, '2012-09-17 13:59:06'),
(1387939, 3147996, '2012-09-17 14:03:01');

SELECT
  ticketid,
  TIME_TO_SEC(TIMEDIFF((
    SELECT t.date FROM ticket_post t WHERE t.ticketid = t1.ticketid AND t.date > t1.date ORDER BY t.date LIMIT 1),
    MIN(date)
  )) diff FROM ticket_post t1
GROUP BY ticketid;

+----------+------+
| ticketid | diff |
+----------+------+
|  1387935 |   40 |
|  1387938 |  120 |
|  1387939 |   24 |
+----------+------+
于 2012-09-17T12:45:26.200 回答
1
select 
  ticketid
  ,time_to_sec(timediff(t2.date, t1.date))  as timediff
from table t1
join table t2 on t1.ticketid=t2.ticketid and t1.ticketpostid<t2.ticketpostid
于 2012-09-17T12:12:52.323 回答