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
 General SQL Server Forums
 New to SQL Server Programming
 unique combination of concatenated column

Author  Topic 

guiz
Starting Member

7 Posts

Posted - 2013-01-28 : 03:17:40
Hi, All. Could you, please, help me with sql query ? For example, if I have a table1 like the following
(fields/values separated by commas):

component, "bin", "prd"
2.1 , "1", "217"
6.5 , "4", "217"
7.1 ,"3", "217"
7.6 ,"5", "217"
7.7 ,"5", "217"
1.3 ,"2", "217"
1.1 ,"1", "298"
3.1 ,"1", "298"
6.2 ,"2", "298"
7.3 ,"5", "298"
8.1 ,"3", "298"
8.4 ,"4", "298"
1.1 ,"5", "298"

and I want to produce a table2 below:

prd , combo
217 , 2.1|1.3|7.1|6.5|7.6
217 , 2.1|1.3|7.1|6.5|7.7
298 , 1.1|6.2|8.1|8.4|1.1
298 , 1.1|6.2|8.1|8.4|7.3
298 , 3.1|6.2|8.1|8.4|1.1
298 , 3.1|6.2|8.1|8.4|7.3

whereas column 'combo' contains all possible unique combinations of components in table1 written in ascending order of table1 'bin' values.

Thank you in advance as your help is very much appreciated.
Cheers,
guiz

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-28 : 03:34:58
[code]
SELECT prd,
STUFF((SELECT '|' + CAST(component AS varchar(10)
FROM table1
WHERE prd = t.prd
ORDER BY [bin]
FOR XML PATH(''))
,1,1,'') AS combo
FROM (SELECT DISTINCT prd FROM table1)t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -