Saikat Saikat - 1 year ago 75
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



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 Source

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

     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 ->

Hope this help!!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download