Medium Challenge 1: PIVOT() Function in Oracle

365 Days of Daily Coding: Day 2

Months of pondering and I am finally embarking on this journey to solve at least one coding challenge a day.

I was reluctant to post about my Day 1 progress but I thought it would be a good idea to document my progress and therefore, somewhere down the line; when I look back, I would appreciate the journey I have gone through. It’s been a rough journey. The starting of the journey was filled with disappointment, frustration and still is. However, with perseverance and grit, I believe I will get there.

I am doing challenges in HackerRank for the start. Today’s challenge really got the best of me.

Given a sample of output in the table name Occupations, I had to pivot the Occupation column so that each name; sorted alphabetically, is displayed under their respective occupation.

Table:

NameOccupation
SamanthaDoctor
JuliaActor
MariaActor
MeeraSinger
AshleyProfessor
Sample of the Occupations table

Expected Output:

DoctorActorSingerProfessor
SamanthaJuliaMeeraAshley
NullMariaNullNull
Sample of the expected output

Solution:

 SELECT Doctor, Professor, Singer, Actor
 FROM (SELECT *
      FROM
      (SELECT Name, Occupation,
      (ROW_NUMBER() OVER(PARTITION BY Occupation ORDER BY Name)) 
      AS row_num
      FROM Occupations
      ORDER BY Name)
      PIVOT (MIN(Name) FOR Occupation in 
      ('Doctor' AS Doctor, 'Professor' AS Professor, 
      'Singer' AS Singer, 'Actor' As Actor))
      ORDER BY row_num);

I used help from the discussion board for the solution as well as to understand the process of how the above solution worked. I have tried to break down the process into several queries to try to explain how the query works.

Query 1:

We can use the Pivot function available in oracle to cross tabulate as well as transpose rows into columns. The oracle documentation to the pivot function is here.

With the following query, the occupation becomes the headers and the aggregate function min takes the first names in the occupation

 SELECT Doctor, Professor, Singer, Actor
 FROM Occupations
 PIVOT
 (MIN(Name) FOR occupation IN ('Doctor' AS Doctor, 'Professor' AS Professor, 'Singer' AS Singer, 'Actor' As Actor)); 

Output:

JuliaSamanthaAshleyMeera
Sample of the query 1 output

Query 2:

With the following query, all the names under the occupation are categorised with row numbers.

SELECT Name, Occupation , ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) AS  row_num FROM Occupations;

Output:

JuliaActor1
MariaActor2
SamanthaDoctor1
AshleyProfessor1
MeeraSinger1
Sample of the query 2 output

Query 3:

With the combination of query 1 and query 2 , you are able to pivot the tables with the occupation as the column header and the row_num as the row headers.

 SELECT *
      FROM
      (SELECT Name, Occupation,
      (ROW_NUMBER() OVER(PARTITION BY Occupation ORDER BY Name)) AS row_num
      FROM Occupations
      ORDER BY Name)
      PIVOT (MIN(Name) FOR Occupation in ('Doctor' AS Doctor, 'Professor' AS Professor, 'Singer' AS Singer, 'Actor' As Actor))
      ORDER BY row_num; 

Output:

1JuliaSamanthaAshleyMeera
2MariaNullNullNull
Sample of query 3 output

Query 4:

Finally, SELECT Doctor, Professor, Singer, Actor FROM selects the column headers leaving the row_nums.

Link to the HackerRank challenge is here.

If you would like to connect, feel free to reach out to me via my social media.

  1. Karma's avatar
  2. Kiran's avatar
  3. Tenzin's avatar
  4. Marelise Jacobs's avatar

    Hi Helo Karma, thank you for your post about this series. It also affected me quite deeply, even though I…

  5. Karma's avatar

One response to “Medium Challenge 1: PIVOT() Function in Oracle”

Leave a comment