选择一行中的行对SQL

时间:2016-06-30 09:42:06

标签: sql oracle partitioning

所以我有这个事件审计表

EventID | EventType | TaskID |   Date             | Iteration
--------------------------------------------------------------
   1    |  start    |   12   |  01/01/2016 09:00  |    1     
--------------------------------------------------------------
   2    |  ended    |   12   |  01/01/2016 09:05  |    1     
--------------------------------------------------------------
   3    |  start    |   14   |  01/01/2016 09:10  |    1     
--------------------------------------------------------------
   4    |  ended    |   14   |  01/01/2016 09:15  |    1
--------------------------------------------------------------
   5    |  start    |   12   |  01/01/2016 09:20  |    2
--------------------------------------------------------------
   6    |  ended    |   12   |  01/01/2016 09:20  |    2
--------------------------------------------------------------
   7    |  ended    |   98   |  01/01/2016 07:14  |    12
--------------------------------------------------------------
   8    |  start    |   66   |  01/01/2016 09:27  |    1

大多数具有不同迭代的任务的开始/结束事件对。但有时只有开始或只有结束行。

我想要的内容:

| TaskID |   Date Started     |   Date ended        |     Iteration 
----------------------------------------------------------------------
|   12   |  01/01/2016 09:00  |    01/01/2016 09:05 |          1     
----------------------------------------------------------------------
|   14   |  01/01/2016 09:10  |    01/01/2016 09:15 |          1
----------------------------------------------------------------------
|   12   |  01/01/2016 09:20  |    01/01/2016 09:20 |          2
----------------------------------------------------------------------
|   98   |         -          |    01/01/2016 07:14 |          12
----------------------------------------------------------------------
|   66   |  01/01/2016 09:27  |        -            |          1

我怎样才能实现这一目标?

Oracle 11g

4 个答案:

答案 0 :(得分:2)

我认为这可以使用自联接:

SELECT tBase.EventId,
       tStarted.Date as DateStarted,
       tEnded.Date as DateEnded,
       tBase.Iteration
FROM  <eventAuditTable> tBase
LEFT JOIN <eventAuditTable> tStarted ON tStarted.eventType = 'Started'
       and tStarted.TaskId = tBase.TaskId
       and tStarted.Iteration = tBase.Iteration
LEFT JOIN <eventAuditTable> tEnded ON tEnded.eventType = 'Ended'
       and tBase.TaskId = tEnded.TaskId
       and tBase.Iteration = tEnded.Iteration

将名称<eventAuditTable>更改为真实姓名并试用!

答案 1 :(得分:2)

试试这个: 测试数据

has_many :icons, -> { order('position_id ASC') } #showing error here..

查询

   with t(EventID,  
     EventType,
        TaskID,
        Dates,
        Iteration) as
         (select 1,
                 'start',
                 12,
                 to_date('01/01/2016 09:00', 'mm/dd/yyyy hh24:mi'),
                 1
            from dual
          union all
          select 2,
                 'ended',
                 12,
                 to_date('01/01/2016 09:05', 'mm/dd/yyyy hh24:mi'),
                 1
            from dual
          union all
          select 3,
                 'start',
                 14,
                 to_date('01/01/2016 09:10', 'mm/dd/yyyy hh24:mi'),
                 1
            from dual
          union all
          select 4,
                 'ended',
                 14,
                 to_date('01/01/2016 09:15', 'mm/dd/yyyy hh24:mi'),
                 1
            from dual
          union all
          select 5,
                 'start',
                 12,
                 to_date('01/01/2016 09:20', 'mm/dd/yyyy hh24:mi'),
                 2
            from dual
          union all
          select 6,
                 'ended',
                 12,
                 to_date('01/01/2016 09:20', 'mm/dd/yyyy hh24:mi'),
                 2
            from dual
          union all
          select 7,
                 'ended',
                 98,
                 to_date('01/01/2016 07:14', 'mm/dd/yyyy hh24:mi'),
                 12
            from dual
          union all
          select 8,
                 'start',
                 66,
                 to_date('01/01/2016 09:27', 'mm/dd/yyyy hh24:mi'),
                 1
            from dual)

如果您想要' - '符号,请尝试

    select TaskID,
           min(case EventType
                 when 'start' then
                  dates
               end),
           max(case EventType
                 when 'ended' then
                  dates
               end),
           Iteration
      from t
     group by TaskID, Iteration

答案 2 :(得分:0)

您也可以使用GROUP BY查询来获得相同的结果:

select TaskId,
       MIN(CASE WHEN EventType = 'start' THEN Date END) as DateStarted,
       MAX(CASE WHEN EventType = 'ended' THEN Date END) as DateEnded,
       MAX(Iteration) as Iteration

from TEvent 
GROUP BY TaskID
ORDER BY TaskID

答案 3 :(得分:0)

试试这个,只需要一个简单的全外连接即可。您可以选择任何您想要的开始或结束的迭代或两者的总和。

select a.TaskID ,a.Date_Started,b.Date_ended,a.Iteration ,b.Iteration    
from event_audit a full outer join event_audit b 
on a.TaskID=b.TaskID;