Solved

Sql Query Question (How to Combine several rows into one row)

Posted on 2009-05-13
5
873 Views
Last Modified: 2012-05-06
I have two tables one table contains order information OrderNumber, ItemNumber, ItemDescription and things like that. The other table contains Shipment information, Box Number
Date Shipped and Time Shipped. If a Item in Table one has been shipped in multiple boxes say 3 for example I do not want to get three rows returned with the only difference being a box number I want to get One row returned and it contain all three box numbers and the Latest Date Shipped

Here is what I get now

Item Number                             Box Number         Date Shipped
   55532                                         234                    4/3/2009
   55532                                         548                    4/12/2009
   55532                                         762                    5/1/2009

Here is what I want to get

ItemNumber                              BoxNumber                  DateShipped
   55532                                      234, 548, 762               5/152009

How do I write my query statement to get the results that I want

I am using Microsoft Sql 2008
0
Comment
Question by:WesleyAlgee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24378848
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 24378891
SELECT ItemNumber, Box =   (SELECT cast(BoxNumber as varchar ) +',' FROM urTable t where  t.ItemNumber = b.itemNumber for XML path('') )
,MAX (ShipDate)
FROM urTable b
GROUP BY ItemNumber
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24379630
Please give this a try. Change table names accordingly.

SELECT 
	a.ItemNumber
	BoxNumber = REPLACE( 
	( 
	SELECT 
	BoxNumber AS [data()] 
	FROM 
	shipmenttable c 
	WHERE 
	c.ItemNumber = b.ItemNumber
	ORDER BY 
	c.ItemNumber 
	FOR XML PATH ('') 
	), ' ', ','),
 
	Max(b.DateShipped)
FROM 
Ordertable a
Inner join shipmenttable b on a.ItemNumber = b.ItemNumber
group by a.ItemNumber

Open in new window

0
 

Author Comment

by:WesleyAlgee
ID: 24380042
ok I will give these a try I will not be able to work on it again until tomorrow
0
 

Author Comment

by:WesleyAlgee
ID: 24560165
I am sorry it took me so long to respond something critical came up that I had to give my attention to
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question