What is the best way to go about returning a result set with the distinct zone values as columns from the below basic select query?
SELECT weight, base_rate, zone
FROM Rates
WHERE modeId = 984
Here is the table:
Rates
table:
id int
modeId int
base_rate decimal
zone varchar
weight decimal
I tried creating a pivot query to accomplish this, but so far have been unsuccessful. Any ideas? Should I just be manipulating the results in the C# code instead? The results will be put into a gridview.
This is what I would like the result set to look like
weight zone1 zone2 zone3 zone4
5 5.50 5.65 5.75 6.00
10 5.55 6.00 7.00 8.00
15 7.50 8.00 9.00 10.00
Here is my failed attempt at creating the pivot query.
select *
from
(
SELECT weight, base_rate, zone FROM Rates where modeId=984
) as d
pivot
(
max(weight)
for zone in (select distinct zone from d)
) piv;