Link to home
Create AccountLog in
Avatar of Junior Vasquez
Junior VasquezFlag for El Salvador

asked on

Pivot Single Column with multiple values

This is just a general question, in SQL can you pivot based on multiple values, separated by commas in one column? 


ex: 


Column Name is Recodes and the values could be (PWID,CPWL,ADW) i know those are the only possible values, so could i make those as pivoted columns and flag 1 or 0 ? 

Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Yes, you can do that rather easily.

Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Just like your last question:
Please post from sample data and the expected results.

I understand PIVOT.

I don't understand what you want to do with 'PWID,CPWL,ADW' and a 0/1 flag.

Please not description or images.  Post typed/pasted text showing what you have and what you want.
Avatar of Junior Vasquez

ASKER

I apologize I don't know how to show the data in fiddle in the final format I need it. I'll try do now

Ok, i built this fiddle


http://sqlfiddle.com/#!18/7e32f/1


the column charge recode can have a value of PWID, ADW or CPWL 


The outcome that i'm looking for is the columns in the table there, plus pivitoted columns PWID, ADW and CPWL


Sometimes an arrest will have more than one in the column charge recode. I would like to pivot and mark 1 if the arrest number has one of those values, or 0 if now. I don't care about the null values. 



>> I don't know how to show the data in fiddle

I don't see where I asked for a fiddle.

I asked for example data and desired results.

What I'm looking for is a simple example of the data you have and the result from that data that you want.

Type it in here.

For example:

I have a columns called col1,col2,col3 with a values of:
'xxx','PWID,CPWL,ADW',99999999999

I need SQL to covert it into:
<type your results here>
I should also ask:
Are you posting sensitive data on a public website?

The data in this question and your previous one looks like real data that might be sensitive in nature.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
thank you for comment. but nothing sensitive or pii data

Thank you all! 


I'd like to split the solution, I hope I can do that.