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
 Multiple results in one row
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

soulchyld21
Starting Member

United Kingdom
27 Posts

Posted - 05/10/2012 :  08:36:05  Show Profile  Reply with Quote
Hi, I have a table with lots of records for a specific customer eg

custnum orderid
205443 384899
205443 372828
205443 361467
205443 372828

I would like to structure a query that will retrieve the results for customer 205443 in such a way that all the different order numbers appear in the same cell or even different cells but in the same row, Is this possible?
This would save me from displaying the customer number multiple times as show above making the data easier to read

a visual representation of what Im trying to achieve might look something like this

custnum orderid
205443 384899,372828,361467,372828

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 05/10/2012 :  08:40:05  Show Profile  Reply with Quote
see http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254


KH
Time is always against us

Go to Top of Page

soulchyld21
Starting Member

United Kingdom
27 Posts

Posted - 05/10/2012 :  09:01:41  Show Profile  Reply with Quote
Thanks, How then would I select all the records pertaining to a custnum and union those records? bearing in mind I may have thousands of customers all with multiple records!

Many thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22460 Posts

Posted - 05/10/2012 :  09:09:51  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
You just use the select statement alone and use your table name

Madhivanan

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

soulchyld21
Starting Member

United Kingdom
27 Posts

Posted - 05/10/2012 :  09:22:42  Show Profile  Reply with Quote
SELECT distinct custnum, STUFF((SELECT ',' + orderid FROM MyTable FOR XML PATH('')), 1, 1, '') AS 'Products Purchased'

FROM MyTable
order by custnum
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 05/10/2012 :  12:00:38  Show Profile  Reply with Quote
quote:
Originally posted by soulchyld21

SELECT distinct custnum, STUFF((SELECT ',' + CAST(orderid AS varchar(10)) FROM MyTable FOR XML PATH('')), 1, 1, '') AS 'Products Purchased'

FROM MyTable
order by custnum



Add this small modification if orderid is of numeric type

------------------------------------------------------------------------------------------------------
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