Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pivot Tables In Sql Server With Spaces In Cell

My example is similar to this - The only problem is I cannot rewrite this query to cope with columns that have spaces inside it

In the example below suppose that rather then [Oranges] you had ['Oranges And Apples'] in one cell.

For some reason adding an "'" means the pivot function returns NULL everywhere and [Oranges And Apples] is of course not valid

What am I doing thats wrong here?

http://www.mssqltips.com/tip.asp?tip=1019

SELECT SalesPerson, [Oranges] AS Oranges, [Pickles] AS Pickles
FROM
(SELECT SalesPerson, Product, SalesAmount
FROM ProductSales ) ps
PIVOT
(
SUM (SalesAmount)
FOR Product IN
( [Oranges], [Pickles])
) AS pvt
like image 831
Jack Kada Avatar asked Mar 12 '10 10:03

Jack Kada


1 Answers

You don't need the apostrophes.

( [Oranges and Apples], [Pickles])
like image 170
Rob Farley Avatar answered Sep 29 '22 00:09

Rob Farley