SQL pivot unpivot

I need to change this

Commissioner Code               Reason Access Provided
1512                                 Responsible Commissioner
1512                                 Resident
1512                                 Mentioned
1412                                 Responsible commissioner
1412                                 Mentioned

into

Commissioner Code       Responsible_Commissioner       Resident            Mentioned
1512                                                              1                             1                    1
1412                                                              1                             0                    1

Any help appreciated.

Cheers

T
LVL 3
trawleyAsked:
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.

SharathData EngineerCommented:
It depends on how many "Reason Access Provided" you have. if you have a finite number of reasons, you can try like this.
select [Commissioner Code],
       max(case [Reason Access Provided] when 'Responsible Commissioner' then 1 else 0 end) as Responsible_Commissioner,
	   max(case [Reason Access Provided] when 'Resident' then 1 else 0 end) as Resident,
	   max(case [Reason Access Provided] when 'Mentioned' then 1 else 0 end) as Mentioned
  from your_table
 group by [Commissioner Code]

Open in new window

0
David ToddSenior DBACommented:
Hi,

Where you really wanting to use the pivot operator, or does Sharath's answer above fit the bill?

What version of SQL are you using - just to confirm that its at least 2005 for the pivot operator.

Regards
  David
0
Tony303Commented:
I have kinda got there.
I put your data into a table called PivotExample.

Struggling to get the individual Commissioner Code in the result.



SELECT [Responsible Commissioner],[Resident],[Mentioned]
FROM
(SELECT [Commissioner Code], [Reason Access Provided]
    FROM [PivotExample] ) AS SourceTable
PIVOT
(
COUNT([Commissioner Code]) 
FOR [Reason Access Provided] IN ([Responsible Commissioner],[Resident],[Mentioned])
) AS PivotTable;

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Tony303Commented:
Got it...

SELECT cc, [Responsible Commissioner],[Resident],[Mentioned]
FROM
(SELECT [Commissioner Code], [Commissioner Code] as CC,  [Reason Access Provided]
    FROM [PivotExample] ) AS SourceTable
PIVOT
(
COUNT([Commissioner Code]) 
FOR [Reason Access Provided] IN ([Responsible Commissioner],[Resident],[Mentioned])
) AS PivotTable;

Open in new window

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
trawleyAuthor Commented:
Thanks all

Will test em out shortly.
0
trawleyAuthor Commented:
Thanks all. I wanted specifically a pivot example and whilst all of the other anwsers were good this one was the best for the particular requirement in mind.

I havent used pivot unpivot yet so wanted to see how it worked.

Thanks Tony

T
0
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 2008

From novice to tech pro — start learning today.