0

I have a data set where 1 LICENSE_ID has been split into multiple rows. I would like to combine the rows so I have 1 row per PERSON_ID and LICENSE_ID combination.

Here is how my data is formatted:

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

The table looks like this:

enter image description here

The output I need is like this:

enter image description here

1 Answer 1

1

You can use conditional aggregation:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.