oracle 用于迭代一列值并获取其计数的SQL查询

ddarikpa  于 2023-03-17  发布在  Oracle
关注(0)|答案(3)|浏览(110)

我在Oracle中有一个名为“transaction_logs”的表,其中包含以下列:
1.作业标识
1.日志消息标识
1.日志消息
1.创建日期
上表中的数据如下:

我希望通过SQL查询从该表获得的输出为:

任何人都可以请帮我写一个SQL查询,以实现这种类型的场景。提前感谢!!

wnavrhmk

wnavrhmk1#

猜测日期包含HH24:MI:SS,您可以尝试此日期,......等待您提供有关DDL的更多信息:

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
;
ekqde3dh

ekqde3dh2#

您需要定义行的ORDER的内容,因此我假设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
oxcyiej7

oxcyiej73#

使用regexp_replacelog_message中提取数字,然后使用LEAD从下一行中获取提取的数据:

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

Demo here

相关问题