Skip to content

Instantly share code, notes, and snippets.

@QuatoHub
Created March 26, 2022 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuatoHub/d1cb314f277ad3f9691e2760342e8fae to your computer and use it in GitHub Desktop.
Save QuatoHub/d1cb314f277ad3f9691e2760342e8fae to your computer and use it in GitHub Desktop.
SET @r1=0, @r2=0, @r3=0, @r4=0;
SELECT MIN(Doctor), MIN(Professor), MIN(Singer), MIN(Actor)
FROM(
SELECT
CASE
WHEN Occupation="Doctor" THEN (@r1:=@r1+1)
WHEN Occupation="Professor" THEN (@r2:=@r2+1)
WHEN Occupation="Singer" THEN (@r3:=@r3+1)
WHEN Occupation="Actor" THEN (@r4:=@r4+1)
END AS RowNumber,
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
) temp
group by RowNumber;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment