2

我有一个看起来类似于这里的表:

DOCCODE  |  DOCDATE  |  STATUS    
001      | 10-OCT-13 |  START    
001      | 12-OCT-13 |  STOP    
001      | 15-OCT-13 |  START    
001      | 20-OCT-13 |  STOP    
002      | 01-NOV-13 |  START

我想试试这个输出

DOCCODE   | STARTDATE  |  STOPDATE  | STATUS    
001       | 10-OCT-13  |  12-OCT-13 | STOP    
001       | 15-OCT-13  |  20-OCT-13 | STOP    
002       | 01-NOV-13  |            | START
4

3 回答 3

0
SELECT s1.doccode AS doccode, 
   s1.docdate AS startdate, 
   s2.docdate AS stopdate, 
   isnull(s2.status, s1.status) AS status
FROM   my_table s1
LEFT OUTER JOIN my_table s2 ON s1.doccode = s2.doccode
WHERE  s1.doccode = 'START' AND s2.doccode = 'STOP'
于 2013-10-23T08:45:09.800 回答
0

您可以通过自我(外部)加入来实现这一点

SELECT start.doccode AS doccode, 
       start.docdate AS startdate, 
       stop.docdate AS stopdate, 
       COALESCE(stop.status, start.status) AS status
FROM   my_table start
LEFT OUTER JOIN my_table stop ON start.doccode = stop.doccode
WHERE  start.doccode = 'START' AND stop.doccode = 'STOP'
于 2013-10-22T07:58:12.597 回答
0

尝试这个:

select sr.DOCCODE
, sr.DOCDATE [STARTDATE]
, x.DOCDATE [STOPDATE]
, ISNULL(x.[STATUS], sr.[STATUS]) [STATUS]
from my_table sr
outer apply (
    select top 1 *
    from my_table sp
    where [STATUS] = 'STOP'
    and DOCDATE > sr.DOCDATE
    and DOCCODE = sr.DOCCODE
    order by DOCDATE) x
where sr.[STATUS]  = 'START'

结果:

DOCCODE STARTDATE   STOPDATE    STATUS
001     2013-10-10  2013-10-12  STOP
001     2013-10-15  2013-10-20  STOP
002     2013-11-01  NULL        START

SQL小提琴


更新

Oracle 版本(使用相关子查询而不是交叉应用):

select sr.DOCCODE
, sr.DOCDATE STARTDATE
, (
    select min(DOCDATE)
    from my_table sp
    where STATUS = 'STOP'
    and DOCDATE > sr.DOCDATE
    and DOCCODE = sr.DOCCODE) as STOPDATE
, NVL((
    select min(STATUS)
    from my_table sp
    where STATUS = 'STOP'
    and DOCDATE > sr.DOCDATE
    and DOCCODE = sr.DOCCODE), sr.STATUS) as STATUS
from my_table sr
where sr.STATUS  = 'START'

SQL 小提琴(甲骨文)

于 2013-10-22T08:03:25.437 回答