Hi,
I feel a bit of an idiot not being able to work out this simple problem, but the documentation seems a little incomprehensible.
I have this data:
declare @pivottest table (strUserId nvarchar(20), strProductId nvarchar(20) )
insert into @pivottest values ( 'A12345', 'product1' )
insert into @pivottest values ( '12345', 'product1' )
insert into @pivottest values ( 'A5678', 'product2' )
insert into @pivottest values ( '5678', 'product2' )
Which results in
strUserId strProductId
-------------------- --------------------
A12345 product1
12345 product1
A5678 product2
5678 product2
But what I actually want is this:
product1 product2
-------------------- --------------------
A12345 A5678
12345 5678
My feeble attempt to produce this result looks like this:
select * from
( select strUserId, strProductId from @pivottest ) sq
pivot
(
max(strUserId)
for strProductId in ([product1],[product2])
) piv
But that just gives me one row. How can I get them all?
Cheers,
Matt