Changing SQL Query Output from Columns to Row Headers

I have a table that contains actor assignments that I need to display as column headers and count how many of those assignments an actor has participated in,

This is my sample query:

SELECT TOP 5
a_ActorIdentifierName as Actor
      ,a_AssignmentsIdName as Assignments
      ,a_partsandincrementsId as AssignmentsID
  FROM a_partsandincrements
  WHERE a_ActorID IN ('11122,'22233') 											

Open in new window


This is the output of my sample query.

Actor	                   Assignments	                  AssignmentsID
Arr, Andrew	Full Swing	                    23
Arr, Andrew	Media Fee	                    32
Arr, Andrew	Extra Risk	                                      43
Arr, Andrew	Understudy Principal	   15
Arr, Andrew	Understudy Principal	   18
Smith, Steve	Full Swing 	                     31
Smith, Steve	Dance Captain 	                     49

Open in new window


I would like for my output to look like this:

Actor	                   Full Swing     Media Fee       Extra Risk       Understudy Principal	Dance Captain 
Arr, Andrew               1                     1                       1                         2                                             0
Smith, Steve               1                    0                        0                         0                                             1

Open in new window

swaggrKAsked:
Who is Participating?
 
Mark BullockQA Engineer IIICommented:
In the SELECT clause, add a subquery for each column:
SELECT TOP 5
ap.a_ActorIdentifierName as Actor
,(select count(*) from a_partsandincrements as fs where fs.a_ActorIdentifierName=ap.a_ActorIdentifierName and fs.a_AssignmentsIdName='Full Swing') as Full_Swing
,(select count(*) from a_partsandincrements as mf where mf.a_ActorIdentifierName=ap.a_ActorIdentifierName and mf.a_AssignmentsIdName='Media Fee') as Full_Swing
. . .
FROM a_partsandincrements ap
WHERE ap.a_ActorID IN ('11122,'22233') 

Open in new window

0
 
swaggrKAuthor Commented:
I made a mistake in my sample output. The AssignmentIDs are unique.

Actor	                   Assignments	                  AssignmentsID
Arr, Andrew	Full Swing	                    1
Arr, Andrew	Media Fee	                    4
Arr, Andrew	Extra Risk	                                      8
Arr, Andrew	Understudy Principal	   15
Arr, Andrew	Understudy Principal	   15
Smith, Steve	Full Swing 	                     1
Smith, Steve	Dance Captain 	                     3
Smith, Steve	Media Fee	                     4

Open in new window

0
 
swaggrKAuthor Commented:
@Mark Bullock
Your suggestion works but it outputs a row for every actor and when I add DISTINCT, it only outputs one actor.
How can I get the query to output a single row for every actor?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.