cross tab query

I have a table  named playerredemptiomn that has 2 columns

Playerid, redemptiondescription


There are like 10 values for redemption description


I am trying to write a query that will list player id, all the values  for redemption description and the number of occurrences for each one

here is some sample data


playerid       redemptiondescription
 12345          deli
12345            buffet
12345            buffet
12341            deli



the query should return


playerid    deli     buffet
12345         1        2
12341          1       0

Please help
johnnyg123Asked:
Who is Participating?
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.

ButlerTechnologyCommented:
Try the following code:
 Select *
from playerredemptiomn 
pivot (count(redemptiondescription) 
for redemptiondescriptionin ([Deli], [Buffet])) as pvt

Open in new window


The downside is that you need to specify all the potential columns.

Tom
0
johnnyg123Author Commented:
I thought I had seen something that would avoid having to list the potential values because they will change from time to time
0
ButlerTechnologyCommented:
I am not sure of a way to get around of not having to list the columns except by shifting the solution to a stored procedure.
Tom
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

johnnyg123Author Commented:
ok..if I shift to stored proc  how could I do it?
0
ButlerTechnologyCommented:
Before moving to store procedure solution, what technology will you be using to ultimately display the table?  (SQL Select, Web Page, Excel, Access)  I ask the question as perhaps the solution for creating the cross-tab lies within that application that is doing the final output.

Tom
0
johnnyg123Author Commented:
just a one time ad hoc query that I was going to paste results into excel
0
ButlerTechnologyCommented:
It would be quicker to copy/paste or link the raw data to Excel and use Excel's pivot table function to get the results.  If this was something that was going to used on a regular basis then I think investigating a Stored Procedure would be the better plan.  Actually, the Excel Pivot feature would be the better plan as it can re-query the data -- assuming that you are linking to the SQL server.

Tom
0

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
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.