Saikat Saikat - 12 days ago 6
SQL Question

SQL - Column to Row conversion in Report

I have a table with student-name and student-city only. This table is used to find out which student has enrolled online-course from which city.

Table - StudentCity

Name City

Anand Mumbai

Vinod Mumbai

Shailesh Pune

Rupali Delhi

Annavi Pune

Arunabha Kolkata


I need the report in the following format:

Mumbai Pune Delhi Kolkata

Anand

Vinod
Shailesh
Rupali
Annavi
Arunabha


I am trying to find out a way so that I can use recursive SQL for this. But till now have not succeeded.

Can someone help me out on this.

Answer

I take my comment back. You can do this instead :

SELECT
     CASE City WHEN 'Mumbai' THEN Name END  AS Mumbai,
     CASE City WHEN 'Pune' THEN Name END AS Pune,
     CASE City WHEN 'Delhi' THEN Name END AS Delhi,
     CASE City WHEN 'Kolkata' THEN Name END AS Kolkata
FROM tab

You can see this in action here -> http://rextester.com/NYZH51845

Hope this help!!!