This is a question on
Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation. The output column headers should be Doctor, Professor, Singer, and Actor, respectively. Note: Print NULL when there are no more names corresponding to an occupation.
Input Format The OCCUPATIONS table is described as follows:
Column Type Name String Occupation String Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.
Sample Input Name Occupation Samantha Doctor Julia Actor Maria Actor Meera Singer Ashely Professor Ketty Professor Christeen Professor Jane Actor Jenny Doctor Priya Singer
Jenny Ashley Meera Jane Samantha Christeen Priya Julia NULL Ketty NULL Maria
Explanation The first column is an alphabetically ordered list of Doctor names. The second column is an alphabetically ordered list of Professor names. The third column is an alphabetically ordered list of Singer names. The fourth column is an alphabetically ordered list of Actor names. The empty cell data for columns with less than the maximum number of names per occupation (in this case, the Professor and Actor columns) are filled with NULL valu
SET @r1=0, @r2=0, @r3 =0, @r4=0; SELECT MIN(Doctor), MIN(Professor), MIN(Singer), MIN(Actor) FROM (SELECT CASE Occupation WHEN 'Doctor' THEN @r1:=@r1+1 WHEN 'Professor' THEN @r2:=@r2+1 WHEN 'Singer' THEN @r3:=@r3+1 WHEN 'Actor' THEN @r4:=@r4+1 END AS RowLine, CASE WHEN Occupation = 'Doctor' THEN Name END AS Doctor, CASE WHEN Occupation = 'Professor' THEN Name END AS Professor, CASE WHEN Occupation = 'Singer' THEN Name END AS Singer, CASE WHEN Occupation = 'Actor' THEN Name END AS Actor FROM OCCUPATIONS ORDER BY Name) AS t GROUP BY RowLine;
My doubt how come MIN(DOCTOR) print names This is from ..Here in this tutorials it comes as number
Please give me a solid answer .I am in the process of learning mysql