如何显示时间戳之间24小时的时差

时间:2018-06-07 15:23:52

标签: java mysql

我有一个返回响应速度的查询。当timediff计算结果超过24小时时,它可以正常工作。我收到错误" java.sql.SQLException:非法小时值' 31'对于java.sal.Time,键入值"

' 31'随时间戳之间的小时差异而变化。

我不知道从哪里开始。我尝试了timestampdiff()但在尝试运行查询时遇到错误。

我们非常感谢任何意见或建议。

SELECT a.*
FROM
(SELECT
databunker.purchases.id                                           AS 'pur_id',
 databunker.purchases.business                                    AS 'pur_business',
 databunker.purchases.time                                        AS 'pur_time',
 databunker.customers.id                                          AS 'customer_id',
 databunker.customers.phone_number                                AS 'customer_phone_#',
 databunker.customers.is_primary                                  AS 'customer_is_primary',
 map.object_salesforce_id,
 databunker.five9_calls.campaign_name,
 databunker.five9_calls.start_timestamp AS 'start_of_call',
 TIMEDIFF(databunker.five9_calls.start_timestamp, databunker.purchases.time) AS 'speed_to_response'

FROM databunker.purchases
 LEFT OUTER JOIN databunker.customers ON (databunker.purchases.customer_id = databunker.customers.id)
 LEFT OUTER JOIN (SELECT
                    databunker.mappings.object_salesforce_id,
                    databunker.mappings.object_id AS 'map_customer_id'
                  FROM databunker.mappings
                  WHERE databunker.mappings.object_class = 'customer') AS map
   ON (databunker.purchases.customer_id = map.map_customer_id)
 LEFT OUTER JOIN databunker.five9_calls ON (map.object_salesforce_id = 
databunker.five9_calls.salesforce_id)
WHERE databunker.purchases.business = 'uma'
     AND databunker.purchases.outcome_type = 'accepted'
     AND databunker.purchases.time >= curdate() - 1

     AND databunker.five9_calls.start_timestamp >= databunker.purchases.time

GROUP BY
 databunker.purchases.id,
 databunker.purchases.business,
 databunker.purchases.time,
 databunker.customers.id,
 databunker.customers.phone_number,
 map.object_salesforce_id,
 databunker.five9_calls.campaign_name,
 databunker.five9_calls.start_timestamp,
 TIMEDIFF(databunker.five9_calls.start_timestamp, databunker.purchases.time)

 ORDER BY databunker.purchases.id, databunker.purchases.time, 
 databunker.five9_calls.start_timestamp ASC) a

INNER JOIN

(
SELECT
  databunker.purchases.id AS 'pur_id_2',
  MIN(databunker.five9_calls.start_timestamp) AS 'call_time'


FROM databunker.purchases
  LEFT OUTER JOIN databunker.customers ON (databunker.purchases.customer_id = databunker.customers.id)
  LEFT OUTER JOIN (SELECT
                     databunker.mappings.object_salesforce_id,
                     databunker.mappings.object_id AS 'map_customer_id'
                   FROM databunker.mappings
                   WHERE databunker.mappings.object_class = 'customer') AS map
    ON (databunker.purchases.customer_id = map.map_customer_id)
  LEFT OUTER JOIN databunker.five9_calls ON (map.object_salesforce_id = databunker.five9_calls.salesforce_id)
WHERE databunker.purchases.business = 'uma'
      AND databunker.purchases.outcome_type = 'accepted'
      AND databunker.purchases.time >= curdate() - 1

      AND databunker.five9_calls.start_timestamp >= databunker.purchases.time

GROUP BY
  databunker.purchases.id
) b

ON a.pur_id = b.pur_id_2 AND a.start_of_call = b.call_time

enter image description here

1 个答案:

答案 0 :(得分:2)

有“时间”,有“时间跨度”。

MySQL的TIMEDIFF()返回31小时的“时间跨度”等等。

您的Java代码期待“一天中的时间”,当然,不应超过23:59:59(忽略闰秒)。

一种解决方法是说

TO_SECONDS(databunker.five9_calls.start_timestamp) -
TO_SECONDS(databunker.purchases.time)

但不会返回(例如)“31:58:47”;相反,它会给出“115127”。

另一种解决方法是避免使用java.sal.Time并找到一个处理“时间跨度”的库。