oracle 将单行中的多个行值转换为列

ilmyapht  于 2023-04-05  发布在  Oracle
关注(0)|答案(1)|浏览(172)

请将a_message值转换为列。

CREATE TABLE test_rows(
id NUMBER(10), 
entity VARCHAR2(15), 
date_s DATE, 
a_message VARCHAR2(1000));

INSERT INTO test_rows 
VALUES(123, 'rat', sysdate, 
'event==''abc'' action==''add_mgmtmeetingdata'' employee_ids==''1;2'' entity==''mgmtmeeting_rat'' meeting_location==''New York'' meeting_type_code==''MSMT'' 
mgmt_meeting_date==''01/27/2010 00:00:00'' source_id==''ABC'' user==''TAM123'' work_object_id==''12345''  user==''TAM345''');

我尝试了REGEXP_SUBSTR和DECODE,但没有得到结果。
我期待如下:
| 事件|作用|员工ID|实体|会议地点|会议类型代码|
| --------------|--------------|--------------|--------------|--------------|--------------|
| 美国广播公司|add_mgmtmeetingdata|一、二|管理会议率|纽约|MSMT|

f4t66c6m

f4t66c6m1#

你在找这个吗:

SELECT
    id,
    entity,
    date_s,
    REGEXP_SUBSTR(a_message, 'event==''([^'']*)', 1, 1, NULL, 1) AS event,
    REGEXP_SUBSTR(a_message, 'action==''([^'']*)', 1, 1, NULL, 1) AS action,
    REGEXP_SUBSTR(a_message, 'employee_ids==''([^'']*)', 1, 1, NULL, 1) AS employee_ids,
    REGEXP_SUBSTR(a_message, 'entity==''([^'']*)', 1, 1, NULL, 1) AS entity,
    REGEXP_SUBSTR(a_message, 'meeting_location==''([^'']*)', 1, 1, NULL, 1) AS meeting_location,
    REGEXP_SUBSTR(a_message, 'meeting_type_code==''([^'']*)', 1, 1, NULL, 1) AS meeting_type_code
FROM
    test_rows;

注意,我已经测试了更多的行:

CREATE TABLE test_rows(
id NUMBER(10), 
entity VARCHAR2(15), 
date_s DATE, 
a_message VARCHAR2(1000));

INSERT INTO test_rows 
VALUES(123, 'rat', sysdate, 'event==''abc'' action==''add_mgmtmeetingdata'' employee_ids==''1;2'' entity==''mgmtmeeting_rat'' meeting_location==''New York'' meeting_type_code==''MSMT'' mgmt_meeting_date==''01/27/2010 00:00:00'' source_id==''ABC'' user==''TAM123'' work_object_id==''12345''  user==''TAM345''');

INSERT INTO test_rows
VALUES(124, 'cat', sysdate,
'event==''def'' action==''update_info'' employee_ids==''3;4'' entity==''meeting_cat'' meeting_location==''Los Angeles'' meeting_type_code==''UPDT'' mgmt_meeting_date==''02/15/2010 00:00:00'' source_id==''DEF'' user==''JAN567'' work_object_id==''67890'' user==''JAN890''');

INSERT INTO test_rows
VALUES(125, 'bat', sysdate,
'event==''ghi'' action==''remove_data'' employee_ids==''5;6'' entity==''conference_bat'' meeting_location==''Chicago'' meeting_type_code==''RMVD'' mgmt_meeting_date==''03/10/2010 00:00:00'' source_id==''GHI'' user==''FEB234'' work_object_id==''23456'' user==''FEB678''');

相关问题