Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to generate list of unique values in csv column?

Posted on 2014-01-18
2
555 Views
Last Modified: 2014-01-19
I have to work with a non-normalized table in MYSQL which I do not have the ability to alter.

The table has a column (col1) which has comma separated values.

I need to generate a list of distinct values within all rows for col1.

Can someone please provide me a query to do that?

col1
------
a,b,c
b,a,g
g,h

Desired result:
a
b
c
g
h
0
Comment
Question by:SAbboushi
2 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39791863
First of all, MySQL is not really the tool for this. You can use the language of your choice and simply do a split, explode, whatever.

However, if you insist, it can be done :)

First, you would need to define a split function. Here's the code, copied from here: http://injustfiveminutes.com/2012/10/17/split-function-in-mysql/
CREATE FUNCTION SPLIT_STR(
  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '');

Open in new window


Then, you could use this in your SQL:
CREATE TEMPORARY TABLE temp (val CHAR(255));
INSERT INTO temp(val) VALUES (SELECT SPLIT_STR(col1, ",", 1) FROM your-table), (SELECT SPLIT_STR(col1, ",", 2) FROM your-table), (SELECT SPLIT_STR(col1, ",", 3) FROM your-table);
SELECT DISTINCT val FROM temp;

Open in new window


The code is not tested, but you get the idea. You would need more SPLIT_STR calls if you have more than 3 joined values.

HTH,
Dan
0
 

Author Closing Comment

by:SAbboushi
ID: 39793221
K thanks--
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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