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
|
2171 Discussions
|
Please Login in order to post a comment
Finally figured out
SELECT COALESCE(MAX(Doctor), 'NULL') AS Doctor, COALESCE(MAX(Professor), 'NULL') AS Professor, COALESCE(MAX(Singer), 'NULL') AS Singer, COALESCE(MAX(Actor), 'NULL') AS Actor FROM ( SELECT 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, ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) AS rn FROM OCCUPATIONS ) AS temp GROUP BY rn ORDER BY rn;
wow, this was really tuf uwu :3
Oracle option
select max(CASE WHEN occupation = 'Doctor' THEN name END) Doctor, max(CASE WHEN occupation = 'Professor' THEN name END) Professor, max(CASE WHEN occupation = 'Singer' THEN name END) Singer, max(CASE WHEN occupation = 'Actor' THEN name END) Actor from (select name, occupation, row_number() over (partition by occupation order by name asc) rn from occupations ) group by rn order by rn asc ;
select Name||'('||substr(occupation,1,1)||')' as output from occupations union all select 'There are a total of' || count(*) ||' '||occupation||'s.' as output from occupations group by occupation order by output;
MS SQL SERVER
WITH ordered AS ( SELECT Name, Occupation, ROW_NUMBER() OVER(PARTITION BY Occupation ORDER BY Name) AS rn FROM OCCUPATIONS ), ctc as ( SELECT * FROM ( SELECT Name, Occupation, rn FROM ordered ) AS src PIVOT ( MAX(Name) FOR Occupation IN ([Doctor], [Professor], [Singer], [Actor]) ) AS p ) SELECT Doctor, Professor, Singer, Actor FROM ctc ORDER BY rn;