Pivoting and nesting tables

Hi Experts, I have a situation as follows;

I have a table that stores the Year, Tournament, Category, Gender, Player and respective stat Count for tennis players. Using this information I want to present it in a better way using pivot and nested repeaters in ASP.NET. The tables are slam_detail (main data table) and category_detail (stores the names of the categories that players are assessed by)

A picture of the table is below. (I have also attached an Excel file with the data)

Tables with data
My desired output would be as follows;

Required Output
If one query can do it - that would be awesome but I presume 2 queries will be needed then I can nest them to get the required output?

My issue is I'm out of ideas on what I would nest in what and how I would pivot the data.

I'm using SQL Server 2012
takwirirarIT Projects ManagerAsked:
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.

Vikas GargBusiness Intelligence DeveloperCommented:

I could try upto this

SELECT Tournament,category_name,YRS,Gender, [Maria S],[Novak D],[Roger F],[Serena W]
FROM (SELECT SD.Year YRS,SD.Tournament,SD.Gender,SD.Player,SD.Count,CD.category_name FROM slam_detail SD
INNER JOIN category_detail CD
ON SD.Category = CD.category_id) SD
FOR PLAYER IN ([Maria S],[Novak D],[Roger F],[Serena W])) AS PVTTable
order by Tournament desc,category_name

Open in new window

takwirirarIT Projects ManagerAuthor Commented:
This is as far as I have got but one of the main issues is having to hard code the names of the players and it producing rows e.g. an entry for Roger F in the womens - it simply shouldn't be there.

Vikas GargBusiness Intelligence DeveloperCommented:
try this for dynamic thing

--Get distinct values of the PIVOT Column
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
--Prepare the PIVOT query using the dynamic
SET @DynamicPivotQuery =
  N'SELECT Tournament,category_name,YEAR,Gender, ' + @ColumnName + '
    FROM (SELECT SD.Year,SD.Tournament,SD.Gender,SD.Player,SD.Count,CD.category_name FROM slam_detail SD
INNER JOIN category_detail CD
ON SD.Category = CD.category_id) SD
          FOR PLAYER IN (' + @ColumnName + ')) AS PVTTable order by Tournament desc,category_name'
--Execute the Dynamic Pivot Query
print  @DynamicPivotQuery

EXEC sp_executesql @DynamicPivotQuery

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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

takwirirarIT Projects ManagerAuthor Commented:
Wow! That's great Vikas. Is there a way to stop it returning the NULL gaps? i.e. [Roger F] is not a female so really it shouldnt return a row?
takwirirarIT Projects ManagerAuthor Commented:
Eventually this is what I want to display in ASP.NET repeater/repeaters

Desirable Output
Vikas GargBusiness Intelligence DeveloperCommented:

I was able to generate SSRS report of the same view with the above result set.
So I think the data is ok all you need to set is at the .net side.

takwirirarIT Projects ManagerAuthor Commented:
Thank you so much!! I will create another question for the .NET side
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.

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.