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
 Simple Problem!
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

funk.phenomena
Posting Yak Master

Canada
121 Posts

Posted - 09/24/2012 :  14:52:45  Show Profile  Reply with Quote
Hi All !!! I have the following Orders Table. The ORDER_ID is repeated on multiple lines depending on the amount of colours that were ordered:


ORDER_ID; PRODUCT_COLOR

123456 Blue 
123456 Black
123456 Red
123457 Blue
123457 Red
123458 black


I need to have the order ID outputted on a single line, with the colors combined into one field, so the output is as follows:


123456 'Blue + Black + Red'
123457 'Blue + Red'
123458 'Black'

I'm not quite sure how to do this, Any ideas? thanKS!


SELECT ORDER_ID, PRODUCT_COLOR FROM ORDERS



Edited by - funk.phenomena on 09/24/2012 14:53:22

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 09/24/2012 :  15:34:46  Show Profile  Reply with Quote

SELECT ORDER_ID,
STUFF((SELECT ' + ' + PRODUCT_COLOR 
       FROM ORDERS 
       WHERE ORDER_ID = t.ORDER_ID 
       FOR XML PATH(''))
       ,1,3,'') AS Colors
FROM (SELECT DISTINCT ORDER_ID FROM ORDERS) t


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

Go to Top of Page

funk.phenomena
Posting Yak Master

Canada
121 Posts

Posted - 09/24/2012 :  15:47:06  Show Profile  Reply with Quote
Visakh - You've gone nuts with your SQL wizardry!
This works perfect, thanks!!!!!!!!!!!!!!!!!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 09/24/2012 :  16:41:49  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
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.06 seconds. Powered By: Snitz Forums 2000