Oracle SQL 透视多列

Oracle SQL Pivot Multiple Columns

我有一个数据集,其中 1 LICENSE_ID 已拆分为多行。我想合并行,所以每个 PERSON_ID 和 LICENSE_ID 组合有 1 行。

我的数据是这样格式化的:

SELECT '123456789' AS PERSON_ID, '1' AS LICENSE_ID, 'NUMBER' AS FIELD, '123' AS EXPECTED, '124' AS ACTUAL, 'UPDATE' AS ACTION FROM DUAL UNION ALL
SELECT '123456789' AS PERSON_ID, '1' AS LICENSE_ID, 'ISSUE_DT' AS FIELD, '43498' AS EXPECTED, '43498' AS ACTUAL, 'NA' AS ACTION FROM DUAL UNION ALL
SELECT '123456789' AS PERSON_ID, '2' AS LICENSE_ID, 'NUMBER' AS FIELD, '888' AS EXPECTED, '888' AS ACTUAL, 'NA' AS ACTION FROM DUAL UNION ALL
SELECT '123456789' AS PERSON_ID, '2' AS LICENSE_ID, 'ISSUE_DT' AS FIELD, '43498' AS EXPECTED, '' AS ACTUAL, 'UPDATE' AS ACTION FROM DUAL

table 看起来像这样:

我需要的输出是这样的:

您可以使用条件聚合:

select person_id, licence_id,
       max(case when field = 'NUMBER' then expected end) as expected_number,
       max(case when field = 'NUMBER' then actual end) as actual_number,
       max(case when field = 'NUMBER' then actual end) as actual_number,
       max(case when field = 'ISSUE_DT' then expected end) as expected_issue_dt,
       max(case when field = 'ISSUE_DT' then actual end) as actual_issue_dt,
       max(case when field = 'ISSUE_DT' then action end) as actual_issue_dt_action,
from t
group by person_id, licence_id