Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ORACLE SQL查找下一个特定值,将其放在一行中并计算时间差_Sql_Oracle - Fatal编程技术网

ORACLE SQL查找下一个特定值,将其放在一行中并计算时间差

ORACLE SQL查找下一个特定值,将其放在一行中并计算时间差,sql,oracle,Sql,Oracle,我有这样一个数据库: ---------------------------------------- WH ERRORDATE ERRORTIME ERRORCODE ------------------------------------------ 658 | 20210503 | 121513 | M1 | 658 | 20210503 | 121613 | M0 | 658 | 20210503 | 134220 |

我有这样一个数据库:

----------------------------------------
WH   ERRORDATE   ERRORTIME   ERRORCODE
------------------------------------------
658 |   20210503 |   121513 |       M1  |
658 |   20210503 |   121613 |       M0  |
658 |   20210503 |   134220 |       M1  |
658 |   20210503 |   134240 |       RH8 |
658 |   20210503 |   134310 |       M0  |
658 |   20210503 |   135011 |       M1  |
658 |   20210503 |   135004 |       M0  |
658 |   20210504 |   071250 |       M0  |
658 |   20210504 |   081513 |       LTZ |
658 |   20210504 |   101343 |       M1  |
658 |   20210504 |   101520 |       M0  |
------------------------------------
--------------------------------------------------------------------------------------------------------
WH   M1ERRORDATE   M1ERRORTIME   M1ERRORCODE  M0ERRORDATE   M0ERRORTIME   M0ERRORCODE  DIFFBETWEEM2TIMES
---------------------------------------------------------------------------------------------------------
658 |   20210503 |      121513 |     M1   |     20210503   |    121613    |     M0     |         ??
658 |   20210503 |      134220 |     M1   |     20210503   |    134310    |     M0     |         ??
658 |   20210503 |      135011 |     M1   |     20210503   |    135004    |     M0     |         ??
658 |   20210504 |      101343 |     M1   |     20210504   |    101520    |     M0     |         ??
-------------------------------------------------------------------------------------------------
我想要的是M1和下一个M0,就像这样:

----------------------------------------
WH   ERRORDATE   ERRORTIME   ERRORCODE
------------------------------------------
658 |   20210503 |   121513 |       M1  |
658 |   20210503 |   121613 |       M0  |
658 |   20210503 |   134220 |       M1  |
658 |   20210503 |   134240 |       RH8 |
658 |   20210503 |   134310 |       M0  |
658 |   20210503 |   135011 |       M1  |
658 |   20210503 |   135004 |       M0  |
658 |   20210504 |   071250 |       M0  |
658 |   20210504 |   081513 |       LTZ |
658 |   20210504 |   101343 |       M1  |
658 |   20210504 |   101520 |       M0  |
------------------------------------
--------------------------------------------------------------------------------------------------------
WH   M1ERRORDATE   M1ERRORTIME   M1ERRORCODE  M0ERRORDATE   M0ERRORTIME   M0ERRORCODE  DIFFBETWEEM2TIMES
---------------------------------------------------------------------------------------------------------
658 |   20210503 |      121513 |     M1   |     20210503   |    121613    |     M0     |         ??
658 |   20210503 |      134220 |     M1   |     20210503   |    134310    |     M0     |         ??
658 |   20210503 |      135011 |     M1   |     20210503   |    135004    |     M0     |         ??
658 |   20210504 |      101343 |     M1   |     20210504   |    101520    |     M0     |         ??
-------------------------------------------------------------------------------------------------
我如何用SQL查询解决这个问题?我想从一个新的角度来做这件事。
我希望M1和下一个M0在同一行中,并计算M1ERRORDATE+M1ERRORTIME和M0ERRORDATE+M0ERRORTIME之间的差值。如果我理解正确,您希望在列中枚举值:

select wh,
       max(case when errorcode = 'M1' then errordate end) as m1_errordate,
       max(case when errorcode = 'M1' then errortime end) as m1_errortime,
       max(case when errorcode = 'M0' then errordate end) as m0_errordate,
       max(case when errorcode = 'M0' then errortime end) as m0_errortime
from (select t.*,
             row_number() over (partition by wh order by errordate, errortime) as seqnum
      from t
      where errorcode in ('M1', 'M0')
     ) t
group by wh, seqnum;
注:

  • errorcode列是多余的,所以我没有包括它
  • 我没有包括差异栏。我让你把它加进去

你试过超前还是滞后?关于pivot/unpivott的研究结果如下:在同一行中列出m1和m0,但如果m1有一个值,m0是(null),如果m0有一个值,m1是(null)在同一行中。因此,在它旁边放(null)也是一样的:(