在一行中选择多对行 SQL
Selecting pairs of rows in one row SQL
所以我有这个事件审计table
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
大多数情况下,有成对的 started/ended 个具有不同迭代的任务事件。但有时只有开始或结束的行。
我想要得到的:
| 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
我怎样才能做到这一点?
甲骨文 11g
我认为这可以使用自连接:
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>
改成真名试试吧!
试试这个:
测试数据
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
如果你想要'-'符号,那么试试
nvl(to_char(min(case EventType
when 'start' then
dates
end),'mm/dd/yyyy hh24:mi'),' -')
您也可以使用 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
试试这个,只需一个简单的完全外部联接就可以得到这个。您可以选择您想要的迭代开始或结束或两者的总和。
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;
所以我有这个事件审计table
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
大多数情况下,有成对的 started/ended 个具有不同迭代的任务事件。但有时只有开始或结束的行。
我想要得到的:
| 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
我怎样才能做到这一点?
甲骨文 11g
我认为这可以使用自连接:
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>
改成真名试试吧!
试试这个: 测试数据
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
如果你想要'-'符号,那么试试
nvl(to_char(min(case EventType
when 'start' then
dates
end),'mm/dd/yyyy hh24:mi'),' -')
您也可以使用 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
试试这个,只需一个简单的完全外部联接就可以得到这个。您可以选择您想要的迭代开始或结束或两者的总和。
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;