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.
Microsoft SQL Server

Avatar of undefined
Last Comment
akscott

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Brian Crowe

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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:

Split.Text.jpg
Brian Crowe

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

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

ASKER
Query Result
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Brian Crowe

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.
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.
Vitor Montalvão

akscott, do you still need help with this question?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
akscott

ASKER
Thank you! Very helpful.