Link to home
Start Free TrialLog in
Avatar of akscott
akscott

asked on

SQL Query for multiple value (comma separated) field

I have a survey table for which I want to count all of the "reasons" a customer is not buying from us.  

Two main columns and samples are:

Survey_ID : ReasonBuying

1 : Price, Other
2 : Price, Service, Quality
3 : Service, Relationship

I want to create a report that counts each response, so how do I get these columns to:

1 : Price
1 : Other
2 : Price
2 : Service
2 : Quality
3 : Service
3 : Relationship

Please help and thank you.
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of akscott
akscott

ASKER

Hi Brian; thank you for taking the time.  I've never actually used a function!  Had to do some reading on that -- I'm mostly just a query and report kind of person.  So, after doing some reading, I ran the script to create the function, then I tried customizing what you did in the top window to how I thought it fit with my stuff and I just get an error message.  I think I'm missing the link in my brain as to how the function actually interacts with the query to convert the comma separated field.  Can you tell me where I've gone wrong here:

User generated image
Gonna do a little trouble-shooting.  Run this and tell me if it works.

SELECT * FROM dbo.SplitText('a,b,c,d,e,f', ',')
Avatar of akscott

ASKER

User generated image
That shows that the function is working correctly.  verify the datatype of sysdba.accountroomsurvey.ReasonBuying.  Also you will want to select Split.Value instead of ReasonBuying.
Avatar of akscott

ASKER

Aha, now we're getting somewhere.  I added an LTrim to remove the spaces that were showing up in the split results and it looks like I'm off to the races. THANK YOU, Brian.
akscott, do you still need help with this question?
Avatar of akscott

ASKER

Thank you! Very helpful.