SQL Query查找两次之间的差异

时间:2017-04-07 11:09:52

标签: sql oracle

DATE                  |  ID |    PARAM_TYPE
07-08-2012 10:10:53 AM| 5613|   INPUT
07-08-2012 10:10:59 AM| 5613|   OUTPUT
07-08-2012 10:16:34 AM| 5624|   INPUT
07-08-2012 10:16:42 AM| 5624|   OUTPUT
07-08-2012 08:09:08 AM| 5503|   INPUT
07-08-2012 08:09:15 AM| 5503|   OUTPUT

表名:EMP_RECORDS 我想将INPUTOUTPUT之间的时差显示为单一记录。

例如:

   DATE               |ID   |  PARAM_TYPE
07-08-2012 10:10:53 AM| 5613|   INPUT
07-08-2012 10:10:59 AM| 5613|   OUTPUT

AS

ID  |Difference
5613| 6 min

4 个答案:

答案 0 :(得分:1)

您可以使用SELECT id, ( output_date - input_date ) * 24 * 60 AS minutes_difference FROM ( SELECT id, "DATE" as output_date, LAG( CASE param_type WHEN 'INPUT' THEN "DATE" END ) IGNORE NULLS OVER ( PARTITION BY id ORDER BY "DATE" ) AS input_date, param_type FROM EMP_RECORDS ) WHERE param_type = 'OUTPUT'; 分析函数:

firebase functions:config:get

答案 1 :(得分:0)

您可以使用连接,其中一个表具有输入,一个具有输出:

SELECT a.ID AS ID,(b.DATE-a.DATE) AS Difference
FROM EMP_RECORDS AS a
     JOIN EMP_RECORDS AS b ON a.ID = b.ID
WHERE a.PARAM_TYPE = 'INPUT' AND b.PARAM_TYPE = 'OUTPUT'

答案 2 :(得分:-1)

我会像这样加入他们(写完,未经测试):

SELECT t1.ID as ID, TIMESTAMPDIFF(MINUTE, t1.DATE, t2.DATE) as difference FROM table t1 INNER JOIN table t2 ON (t2.ID=t1.ID AND t2.PARAM_TYPE = 'OUTPUT') WHERE t1.PARAM_TYPE = 'INPUT';

在此处阅读更多内容:https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampdiff

答案 3 :(得分:-1)

PLS更改列名称日期:仅适用于SQL

curl