Pivot Select?

Posted on 2011-10-31
Last Modified: 2012-05-12
I need to build a select based on two columns

team     disqReason
1           Bad
1           Good
1           No Money
2           Good

With the results

Team      Bad        Good     No Money
1              1               1            1
2              0               1            0
Question by:lrbrister
    LVL 22

    Expert Comment

    DECLARE @t table (team int, disqReason varchar(100))
    INSERT @t 
    SELECT 1, 'Bad'
    UNION ALL SELECT 1, 'Good'
    UNION ALL SELECT 1, 'No Money'
    UNION ALL SELECT 2, 'Good'
    SELECT *
    FROM @t T
    pivot (COUNT(disqReason) FOR disqReason IN ([Bad], [Good], [No Money])) pvt

    Open in new window

    LVL 15

    Accepted Solution

    Try this.
    	Team, [Bad], [Good], [No Money]
    		Team, disqReason
    		YourTable) p
    	PIVOT (
    	COUNT(team) FOR disqReason IN ([Bad], [Good], [No Money])
    	) pvt

    Open in new window


    Author Closing Comment

    Perfect and what I asked for

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now