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:

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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BullockQA EngineerCommented:
In the SELECT clause, add a subquery for each column:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

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?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.