15,303,572 members
See more:
Here is my Database structure.

```  source      destination         vehicle              Rate_New
-------------------------------------------------- ----------------------------
A        B           Small                         1400
A        B              Regular                        1750
A        B          Luxury                         2600
C        D          Regular                        3500
C    D           Luxury                        5600
```

I want to display data like this following :

```source   destination      Small                  Regular                Luxury
-------------------------------------------------- ------------------------------
A       B            1400                   1750                   2600
C           D            NULL                   3500                   5600
```

How to do this?
Posted
Updated 28-May-12 23:37pm
v2

## Solution 1

Like this:
SQL
```SELECT [source], [destination], [Small], [Regular], [Luxury]
FROM (SELECT * FROM YourTable) AS DT
PIVOT(SUM(Rate_new) FOR [vehicle] IN ([Small],[Regular],[Luxury])) AS PT
ORDER BY [source]```

More examples: http://www.codeproject.com/search.aspx?q=author%3A+losmac+AND+pivot&x=0&y=0&sbo=qa&usfc=false[^]
v3
Sandeep Mewara 29-May-12 14:14pm

My 5!
Maciej Los 29-May-12 14:35pm

Thank you, Sandeep ;)

## Solution 6

i give you a simple example as i understand your query.

SQL
```SELECT [source], [destination], [Small], [Regular], [Luxury]
FROM
(SELECT *
FROM YourTable) p
PIVOT
(
Sum(SUM(Rate_new)
FOR vehicle IN
( ([Small],[Regular],[Luxury] )
) AS pvt
ORDER BY source```
Maciej Los 30-May-12 2:59am

Why did you copy my answer?

## Solution 3

The correct SQL script would be:
SQL
```SELECT [source], [destination], [Small], [Regular], [Luxury]
FROM
(SELECT [source], destination, vehicle, rate_new FROM TestTable) AS DT
PIVOT(SUM(Rate_new) FOR vehicle IN ([Small],[Regular],[Luxury])) AS PT
ORDER BY [source]```

## Solution 4

SQL
```SELECT source,destination, [Small Car],[Regular Car],[Luxury Car]
FROM
(
SELECT source, destination, vehicle, Rate_New
FROM seasonal_pickupdroptariff
) up
PIVOT (Min(Rate_New) FOR vehicle IN ([Small Car],[Regular Car],[Luxury Car])) AS pvt
order by source```
Maciej Los 30-May-12 2:59am

Why did you copy my answer?
If you have found an answer, it would be good to vote each helpful answer and mark it as "solved".
sahabiswarup 8-Jun-12 6:18am

This is the final query losmac; i just modified your query and get my result.