Declare @table1 table (
SalesPerson varchar(50), Product varchar(50), SalesAmount float )

insert into @table1(SalesPerson,Product, SalesAmount) values (‘Bob’, ‘Pickles’, 100)
insert into @table1(SalesPerson,Product, SalesAmount) values (‘Sue’, ‘Oranges’, 50)
insert into @table1(SalesPerson,Product, SalesAmount) values (‘Bob’, ‘Pickles’, 25)
insert into @table1(SalesPerson,Product, SalesAmount) values (‘Bob’, ‘Oranges’, 300)
insert into @table1(SalesPerson,Product, SalesAmount) values (‘Sue’, ‘Oranges’, 500)

select * from @table1

SELECT SalesPerson, [Oranges] AS Oranges, [Pickles] AS Pickles
FROM
(SELECT SalesPerson, Product, SalesAmount
FROM @table1 ) ps
PIVOT
(
SUM (SalesAmount)
FOR Product IN
( [Oranges], [Pickles])
) AS pvt

Advertisements