0

我正在使用票务系统,并且需要编写一个查询来检查代理是否迟到了他们的交付。

这是我到目前为止所拥有的,也是我需要的:

  • 我有:ticket_id time_created
  • 我需要选择所有相同的票证 ID,其中两者之间的 time_created 大于 24 小时。

例如:

ticket_id time_created 
1         2013-08-19 12:11
1         2013-08-17 12:07
1         2013-08-17 12:00
2         2013-08-19 12:11
2         2013-08-19 12:10

结果

ticket_id time_difference
1             48:04

我只需要检查具有相同ticket_id 号码的门票。

编辑可以说还有 id 列我也需要检查值介于 1,2 和 2,3 之间(在这种情况下 - 所有ticket_id = 1)和 4,5(ticket_id = 2)

4

3 回答 3

0

这样的东西可能会奏效:

select ticket_id, min(a.time_created), max(b.time_created),
       TIMESTAMPDIFF(HOUR,min(a.time_created), max(b.time_created))
  from tbl AS a JOIN tbl AS b USING(ticket_id)
  where a.time_created < (b.time_created - INTERVAL 1 DAY)
  group by(a.ticket_id);

生产:

+------------+--------------------------------+--------------------------------+--------------------------------------------------------------+
| TICKET_ID  |      MIN(A.TIME_CREATED)       |      MAX(B.TIME_CREATED)       | TIMESTAMPDIFF(HOUR,MIN(A.TIME_CREATED), MAX(B.TIME_CREATED)) |
+------------+--------------------------------+--------------------------------+--------------------------------------------------------------+
|         1  | August, 17 2013 12:00:00+0000  | August, 19 2013 12:11:00+0000  |                                                           48 |
+------------+--------------------------------+--------------------------------+--------------------------------------------------------------+

http://sqlfiddle.com/#!2/4af76e/7

请注意我TIMESTAMPDIFF在此处使用,请求HOUR. TIMEDIFF不知道您的域规范,由于值范围有限TIME(≈ 34 天),我不在这里使用。

于 2013-08-19T14:38:20.950 回答
0

要获得差异,您需要等效于lag()MySQL 不支持的函数。您可以使用相关子查询获得相同的效果:

select ticket_id, timediff(nextTimeCreated, time_created)
from (select t.*,
             (select time_created
              from t t2
              where t2.ticket_id = t.ticket_id and
                    t2.time_created > t.time_created
             ) as nextTimeCreated
      from t
     ) t
where nextTimeCreated > time_created + interval 1 day;
于 2013-08-19T14:40:53.927 回答
0

此查询将检查最后日期和最低日期之间的差异,你想要这个还是最后一个日期和倒数第二个日期之间的差异

SELECT ticket_id, 
DATEDIFF(MAX(TIME_CREATED),MIN(TIME_CREATED)) AS DiffDate 
FROM TABLE1 
    group by ticket_id

此查询应该找到两张最新票之间的差异

SELECT ticket_id, TIMEDIFF(MAX(A.TIME_CREATED),

             (SELECT MAX(TIME_CREATED) FROM TICKETS B 
              where B.TIME_CREATED <MAX(A.TIME_CREATED)))  AS DiffDate 
    FROM TICKETS A
        group by ticket_id

想一想,我现在认为这可能是

两个连续行中的日期之间的差异

你可以在那里尝试查询...

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票证) x WHERE 排名 = 2;

并在

http://sqlfiddle.com/#!1/0e213/1

于 2013-08-19T14:44:02.727 回答