Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to generate list of unique values in csv column?

Posted on 2014-01-18
2
Medium Priority
?
566 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
[X]
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
2 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 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

In this article, we’ll look at how to deploy ProxySQL.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

610 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