Liry D
Liry D

Reputation: 11

Switch distinct values in a column into column name

I am trying to create a new table by using the dataset in the link below. The return I want to have is like the picture below. Could someone please help?

enter image description here

http://sqlfiddle.com/#!18/766e3/2

Upvotes: 0

Views: 35

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269823

You can use conditional aggregation:

SELECT Employeeid,
       SUM(CASE WHEN BusinessUniteID = 1 then Scores END) as score_1,
       SUM(CASE WHEN BusinessUniteID = 2 then Scores END) as score_2,
       SUM(CASE WHEN BusinessUniteID = 3 then Scores END) as score_3
FROM t_d
GROUP BY EmployeeId;

This assumes that you know what all the business unit ids are.

Here is a db<>fiddle.

Upvotes: 1

Related Questions