Avatar of swaggrK
swaggrK
 asked on

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

Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
swaggrK

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Mark Bullock

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
swaggrK

ASKER
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

swaggrK

ASKER
@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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23