Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SELECT DISTINCT from a comma seperated column

Author  Topic 

bharath.gct
Starting Member

1 Post

Posted - 2010-08-31 : 20:03:23
Hi All,

I am pretty much new/naive in writing complex SQLs in an effecient way. I have a requirement where I need to write a complex SQL in an effecient way. The requirement is as follows:

I have a table 'testtable' with columns 'testid' and 'testcolumn' which has values like the following:

TESTID	TESTCOLUMN
------ ----------
112 Red,Green
789 Green,Yellow
535 Brown,Red
97 Blue,Green,Cyan
1174 White,Brown,Yellow


I need to write a SELECT query which would give me all the distinct colors present in the column (irrespective of the commas), for all testids < 1000. My output should be something like: (Sorted/Unsorted is fine)

OUTPUT
------
Blue
Brown
Cyan
Green
Red
Yellow


If you look at the output, all colors are present only once (DISTINCT) and White is not present due to the condition (< 1000).

My real table has around 30000 records and I really need an effecient query which can do this job so that there is no performance hit.

Thanks in Advance. Let me know for any questions.

Bharath

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2010-08-31 : 22:15:49
your case is explained well here
http://www.sqlteam.com/article/parsing-csv-values-into-multiple-rows
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-01 : 05:29:08
You need to read about normalization

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -