MySQL使用非结构化数据计算行差异

时间:2014-03-25 12:00:50

标签: mysql select

我有一个包含列的表消息:phonenumber,type,timestamp 类型表示消息是问题还是答案。

现在我需要一个可以计算问题和答案之间响应时间的查询,但问题是问题和答案之间不存在任何关系。回答,除了他们有相同的电话号码。

所以这两个记录属于一起:

Phonenumber | Type     | Timestamp  
  123456789 | Question | xxxxxx  
  123456789 | Answer   | xxxxxx
  • 在将答案发送到该电话号码之前,可能存在来自同一电话号码的多个问题。在这种情况下,应使用上一条消息的时间戳:

    Phonenumber | Type     | Timestamp  
      123456789 | Question | xxxxxx  
      123456789 | Question | xxxxxx  
      123456789 | Question | xxxxxx  <-  
      123456789 | Answer   | xxxxxx
    
  • 不是Q - &gt; A,Q - &gt; A在数据库中,所以这样的事情是可能的:

    Phonenumber | Type     | Timestamp  
      123456789 | Question | xxxxxx  
      987654321 | Question | xxxxxx  
      987654321 | Answer   | xxxxxx  
      123456789 | Answer   | xxxxxx
    
  • 1 Phonenumber可以有多组Q - &gt; A.所有这些集应该在计算中:

    Phonenumber | Type     | Timestamp  
      123456789 | Question | xxxxxx  
      123456789 | Answer   | xxxxxx  
      123456789 | Question | xxxxxx  
      123456789 | Answer   | xxxxxx  
    

说实话,我不知道如何开始这个。我已经找到了一些关于计算2行之间差异的东西,但我需要先将数据完全结构化。 (Q - > A,Q - > A,Q - > A,......)。

我知道如果在Q和相应的A之间定义关系但是没有可用的关系会更容易:/

SQL小提琴链接非结构化数据:http://sqlfiddle.com/#!2/4dff3/1/0
SQL Fiddle链接结构化数据:http://sqlfiddle.com/#!2/61a62/1/0
有了这个,我可以通过计算row2-row1,row4-row3,row6-row5之间的时间戳来计算平均响应时间。

更新:
SELECT t1.answer AS answer, t1.timeOfAnswer AS timeOfAnswer, MIN(t1.responseTime) AS responseTime FROM ( SELECT a.message AS answer, a.timestamp AS timeOfAnswer, timestampdiff(second, q.timestamp, a.timestamp) AS responseTime FROM message q JOIN message a ON q.phoneNumber = a.phoneNumber WHERE q.type = 0 AND a.type = 1 HAVING responseTime > 0 ) AS t1 GROUP BY t1.answer
使用此查询,它可以正常处理我的示例数据,因为邮件内容的名称类似于&#39;邮件1回答&#39;,&#39;邮件2回答&#39;,...所以我可以对此进行GROUP BY领域。但是根据实际数据,可能会多次发送相同的答案。

2 个答案:

答案 0 :(得分:0)

我不确定这是100%正确但它可能是一个有用的开始:

select q0.message, max(q0.tsd) from 
(
   select m0.message, timestampdiff(second, m0.timestamp, m1.timestamp) as tsd 
   from message m0 
   join message m1 on m0.phoneNumber = m1.phoneNumber 
   where m0.type = 1 and m1.type = 0
   having tsd < 0
) as q0  
group by message;

这是一个fiddle来试验。

答案 1 :(得分:0)

基于ethrbunny发布的查询(非常感谢!) 我认为这是解决方案:

SELECT 
  t1.answerId AS answerId, 
  t1.answer AS answer, 
  t1.timeOfAnswer AS timeOfAnswer, 
  MIN(t1.responseTime) AS responseTime 
FROM (
  SELECT
    a.id AS answerId,
    a.message AS answer,
    a.timestamp AS timeOfAnswer,
    timestampdiff(second, q.timestamp, a.timestamp) AS responseTime 
  FROM message q 
  JOIN message a ON q.phoneNumber = a.phoneNumber 
  WHERE q.type = 0 AND a.type = 1
  HAVING responseTime > 0
) AS t1
GROUP BY t1.answerId