Saturday, January 9, 2021

How to insert duplicate records into one table and non-duplicate records into another table using single DML operation (Oracle DB)?

 Input:


NONDUP_REC:

DUP_REC:

Query:

DUP_RECORDS_SPLIT Data:


INSERT FIRST 
WHEN RNK = 1 THEN 
    INTO NONDUP_REC VALUES (SID,SNAME,MARKS)
ELSE 
    INTO DUP_REC VALUES (SID,SNAME,MARKS)
SELECT * FROM
(SELECT SID,SNAME,MARKS, COUNT(SID) OVER (PARTITION BY SID ORDER BY SID) RNK FROM DUP_RECORDS_SPLIT);

COMMIT;


How to convert rows to columns in SQL (Oracle DB)?

 Input:

Expected Output:



Query:

UNPIVOT_TABLE Data:

SELECT * FROM 

    (SELECT * 

        FROM UNPIVOT_TABLE) 

        PIVOT (MAX(SALES) FOR QUARTER IN ('Q1' AS Q1,'Q2' AS Q2,'Q3' AS Q3,'Q4' AS Q4));

How to convert columns into rows in SQL (Oracle Database)?

 Input:


Expected Output:

    



Query:

PIVOT_TABLE Data:

SELECT * FROM 

    (SELECT * 

        FROM PIVOT_TABLE)

        UNPIVOT(SALES FOR QUARTER IN (Q1, Q2, Q3, Q4));