We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- SQL
- Advanced Select
- Occupations
- Discussions
Occupations
Occupations
Sort by
recency
|
2114 Discussions
|
Please Login in order to post a comment
with procte as ( select row_number()over(order by Professor) as rn, Professor from (select CASE WHEN OCCUPATION = 'Professor' THEN Name END Professor from OCCUPATIONS) as occ where Professor is NOT NULL), actcte as ( select row_number()over(order by Actor) as rn, Actor from (select CASE WHEN OCCUPATION = 'Actor' THEN Name END Actor from OCCUPATIONS) as occ where Actor is NOT NULL), singcte as ( select row_number()over(order by Singer) as rn, Singer from (select CASE WHEN OCCUPATION = 'Singer' THEN Name END Singer from OCCUPATIONS) as occ where Singer is NOT NULL), doccte as ( select row_number()over(order by Doctor) as rn, Doctor from (select CASE WHEN OCCUPATION = 'Doctor' THEN Name END Doctor from OCCUPATIONS) as occ where Doctor is NOT NULL) select Doctor, Professor, Singer, Actor from procte p left join actcte a on p.rn = a.rn left join singcte s on p.rn = s.rn left join doccte d on p.rn = d.rn;
--If anyone get stuck_by_this_problem-- SELECT [Doctor], [Professor], [Singer], [Actor] FROM(SELECT Occupation, Name, 'Name' + CAST(RANK() OVER( PARTITION BY Occupation ORDER BY Name) AS VARCHAR(10)) Occu_Rank FROM OCCUPATIONS) Tmp PIVOT (MAX(Name) FOR Occupation IN ([Doctor], [Professor], [Singer], [Actor]) ) pvt ;
SELECT [Doctor], [Professor], [Singer], [Actor] FROM ( select Name, Occupation, rank() over (partition by Occupation order by Name) as rank from OCCUPATIONS) a PIVOT ( MAX(Name) FOR Occupation IN ([Doctor], [Professor], [Singer], [Actor]) ) AS PivotedOccupations
WITH RANKED AS( SELECT NAME,OCCUPATION, ROW_NUMBER()OVER(PARTITION BY OCCUPATION ORDER BY NAME) AS RN FROM OCCUPATIONS ) SELECT [DOCTOR],[PROFESSOR],[SINGER],[ACTOR] FROM(SELECT NAME,OCCUPATION,RN FROM RANKED) AS SOURCE_TAB PIVOT( MAX(NAME) FOR OCCUPATION IN ([DOCTOR],[PROFESSOR],[SINGER],[ACTOR]) )AS PIV_TAB ORDER BY RN;