i have a Table DistanceTable
It has Columns like
Slno From To Dist
-----------------------------
1 Vskp Hyd 100
2 Hyd Chennai 200
3 Chennai Vskp 458
Now i want the data to be displayed as
VSKP HYD CHENNAI
VSKP 0km 100km 458km
HYD 100km 0km 200km
CHANNAI 458km 200km 0km
I tried with the query
SELECT *
FROM ( SELECT slno, fromcity ,tocity
FROM DistanceTable ) AS ET
PIVOT(MIN(slno) FOR fromcity IN (tocity) AS PT
here's a solution (but w/out pivot):
SQL fiddle
Let me explain this issue demonstrating simple example. USE AdventureWorks
Try this :
SQL FIddle
Update : Use dynamic SQL for variable number of cities as below :