If you only have one value for each result, you can use any of the aggregate functions - eg: MAX
select * from
(select t.date, t.data,c.name from t1 t inner join category c on t.category = c.category) s
pivot (max(data) for name in ([b],[c])) p
The first part is the source data
select t.date, t.data,c.name from t1 t inner join category c on t.category = c.category
Then the pivot
rearranges it, creating columns for the in
values from the name
column, and using the aggregate (max(data)
) for the value in that column, which should give the desired results.
If you're getting nulls, it's probably from using select *
in your source query, rather than selecting the required fields - ie: replace
SELECT * FROM T1 WHERE Category >= 2
with
select category, data FROM T1 WHERE Category >= 2