select process_name, msg_count from (
select
last_value(process_name) ignore nulls over(partition by job_id order by date_created) as process_name,
last_value(qty) ignore nulls over(partition by job_id, process_name order by date_created) as msg_count,
date_created
from (
select
job_id, logmsg_id, log_message,
case when instr(log_message,'Processed') = 0 then log_message end as process_name,
case when instr(log_message,'Processed') = 1 then to_number(regexp_substr(log_message, '\d+')) end as qty,
date_created
from data
where logmsg_id = 100
)
)
where msg_count is not null
order by date_created
;
SQL> create table t ( j int, id int, msg varchar2(30), d timestamp );
Table created.
SQL>
SQL> insert into t values (123,100,'ABC',systimestamp);
1 row created.
SQL> insert into t values (123,100,'Processed Msg : 17',systimestamp);
1 row created.
SQL> insert into t values (123,100,'BCD',systimestamp);
1 row created.
SQL> insert into t values (123,100,'Processed Msg : 32',systimestamp);
1 row created.
SQL> insert into t values (123,100,'MNP',systimestamp);
1 row created.
SQL> insert into t values (123,100,'Exceeded limit',systimestamp);
1 row created.
SQL> insert into t values (123,100,'Processed Msg : 28',systimestamp);
1 row created.
SQL>
SQL> select
2 case when length(msg) = 3 then msg end tag,
3 case when msg like 'Processed%' then substr(msg,18) end prc
4 from t
5 order by d;
TAG PRC
------------------------------ ----------------------------------------------------
ABC
7
BCD
2
MNP
8
7 rows selected.
SQL>
SQL> select tag, prc
2 from (
3 select d, lead(prc ignore nulls) over ( order by d ) as prc,
4 tag
5 from (
6 select
7 d,
8 case when length(msg) = 3 then msg end tag,
9 case when msg like 'Processed%' then substr(msg,18) end prc
10 from t
11 )
12 )
13 where tag is not null
14 order by d;
TAG PRC
------------------------------ ----------------------------------------------------
ABC 7
BCD 2
MNP 8
With cte as (
select LOG_MESSAGE AS Process_Name, LEAD(Log_Msg ignore nulls) over (order by DATE_CREATED) as Msg_Count
from (
select t.*, regexp_replace(LOG_MESSAGE, '[^0-9]', '') as Log_Msg
from mytable t
)
)
select *
from cte
where length(PROCESS_NAME) = 3
这是不使用WITH cte的另一种方法:
select *
from (
select LOG_MESSAGE AS Process_Name, LEAD(Log_Msg ignore nulls) over (order by DATE_CREATED) as Msg_Count
from (
select t.*, regexp_replace(LOG_MESSAGE, '[^0-9]', '') as Log_Msg
from mytable t
)
)
where length(PROCESS_NAME) = 3
3条答案
按热度按时间wnavrhmk1#
猜测日期包含HH24:MI:SS,您可以尝试此日期,......等待您提供有关DDL的更多信息:
ekqde3dh2#
您需要定义行的ORDER的内容,因此我假设DATE_CREATED中有一个时间戳或类似的内容
oxcyiej73#
使用
regexp_replace
从log_message
中提取数字,然后使用LEAD
从下一行中获取提取的数据:这是不使用
WITH cte
的另一种方法:Demo here