SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 unique combination of concatenated column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

guiz
Starting Member

7 Posts

Posted - 01/28/2013 :  03:17:40  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 01/28/2013 :  03:34:58  Show Profile  Reply with Quote

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


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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000