Skip to content
Advertisement

Pivot table in alphabetical order

Am new to MySQL, I want to pivot a table
Table contains two columns only Name and Occupation Where no null values are inserted

I want to pivot the occupation column so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation. The output column headers should be Doctor, Professor, Singer, and Actor
I tried this query, Where am able to get them to the point where the column is pivoted

My output as:

I can’t figure a way out how can I can get output like:

If someone could explain it to me, It would be really helpful. Thanks

Advertisement

Answer

You can do this with window functions (available in MySQL 8.0) and aggregation:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement