i have to convert the entire result in Oracle into a single row result set with different column names
I have tried with some self joins but not appears to be working .
Expected output
Advertisement
Answer
Give each row a unique number and then PIVOT
:
Oracle Setup:
CREATE TABLE table_name ( ssn, first_name, last_name ) AS SELECT 123456789, 'VKP', 'KKO' FROM DUAL UNION ALL SELECT 987654321, 'ABC', 'XYZ' FROM DUAL UNION ALL SELECT 332211456, 'PQR', 'GHJ' FROM DUAL
Query:
SELECT "1_SSN" AS SSN_1, "1_FIRST_NAME" AS FIRST_NAME_1, "1_LAST_NAME" AS LAST_NAME_1, "2_SSN" AS SSN_2, "2_FIRST_NAME" AS FIRST_NAME_2, "2_LAST_NAME" AS LAST_NAME_2, "3_SSN" AS SSN_3, "3_FIRST_NAME" AS FIRST_NAME_3, "3_LAST_NAME" AS LAST_NAME_3 FROM ( SELECT t.*, ROWNUM AS rn FROM table_name t ) PIVOT( MAX( SSN ) AS SSN, MAX( FIRST_NAME ) AS first_name, MAX( LAST_NAME ) AS last_name FOR rn IN (1, 2, 3) )
Output:
SSN_1 | FIRST_NAME_1 | LAST_NAME_1 | SSN_2 | FIRST_NAME_2 | LAST_NAME_2 | SSN_3 | FIRST_NAME_3 | LAST_NAME_3 --------: | :----------- | :---------- | --------: | :----------- | :---------- | --------: | :----------- | :---------- 123456789 | VKP | KKO | 987654321 | ABC | XYZ | 332211456 | PQR | GHJ
db<>fiddle here