SQL Query for pivot and calculation.

Am trying to write a sql query to get the data in a particular format. Please see my actual data below and the result am trying to get.

Sport            Team                No of Females                No of males

Baseball                    Varsity                             20                                                 0
Baseball                     Varsity                                                                                 20
Football                    Varsity                                                                                   15

Above is the format of the table and data in the actual database.
Now i want to get the data in this below format.

                                  Female Only                 Male only                   Total
Sports                           1                                    2                                  2
Teams                           1                                    2                                 3
Participants                20                                 35                                 55

Please help me with the sql query , I tried using pivot and count of the columns but i couldn't achieve the way i wanted. Thanks for your help!.
Elizabeth GAsked:
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.

Scott PletcherSenior DBACommented:
Something like below, I think.  I'm allowing for the table to be able to have non-sports rows as well (Sport = '').  If that's not the case, you won't need the test for Sports or perhaps for Teams.

    oa1.Category AS [ ],
    SUM(CASE WHEN [No of Females] > 0 AND [No of Males] = 0
             THEN CASE WHEN oa1.Category = 'Participants' THEN [No of Females] ELSE 1 END ELSE 0 END) AS [Female Only],
    SUM(CASE WHEN [No of Females] = 0 AND [No of Males] > 0
             THEN CASE WHEN oa1.Category = 'Participants' THEN [No of Males] ELSE 1 END ELSE 0 END) AS [Male Only],
    SUM(CASE WHEN ([No of Females] > 0 AND [No of Males] = 0) OR([No of Females] = 0 AND [No of Males] > 0)
             THEN CASE WHEN oa1.Category = 'Participants'
                       THEN [No of Females] + [No of Males] ELSE 1 END ELSE 0 END) AS [Total]
FROM dbo.table_name
    SELECT 'Sports' AS Category
    WHERE Sport > ''
    SELECT 'Teams'
    WHERE Teams > ''
    SELECT 'Participants'
    WHERE Sport > '' AND Teams > ''
) AS oa1(Category)
GROUP BY oa1.Category

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
Elizabeth GAuthor Commented:
Thank you so much !. I got it to work with the help of your Query.
Scott PletcherSenior DBACommented:
You're welcome!  (Btw, this q of yours put me over 10M point mark, Thanks!)
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.