Click here to Skip to main content
16,012,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends

Can any one suggest me , I need to merge the multiple columns of a repeated rows into a single column of a single with SEMICOLON using sql query .


For EXAMPLE:
The table contains the column and their value is ,

SerialNumber       ProductNumber         ProductNumber
   QC12345             BA98765             1
   QC67890             BA98765             3
   QC00000             BA98765             1
   QC00011             BA12345             2
   QA01234             BA12345             2
   .                       .                       .
   .                       .                       .
   .                       .                       .
   .                       .                       .


The ABOVE VALUE IS JUST EXAMPLE , VALUES IS UNKNOWN , IT MIGHT BE MORE THAN 100 OR 200 OR EVEN 1000 RECORDS MAY BE AVAILABLE BUT

the OUTPUT FORMAT which I want is :
     SerialNumber             ProductNumber       Quantity
QC12345;QC67890;QC00000          BA98765              5
  QC00011;QA01234                BA12345              4



So can any one provide me a Query to create output as like the above format.
Posted
Updated 14-Oct-11 21:39pm
v3

This is a bad idea.

You are better off using two separate queries one for the count on product numbers and the other for product number serial numbers.
 
Share this answer
 
Comments
RaviRanjanKr 16-Oct-11 3:09am    
Appreciating Idea, MY 5+
Mehdi Gholam 16-Oct-11 3:14am    
Thanks, Ravi.
Based on the answer[^] to a similar question[^] I think the query should look something like this:
SQL
SELECT REPLACE(RTRIM((SELECT SerialNumber + ' ' FROM myTable
                      WHERE (ProductNumber = T.ProductNumber)
                      FOR XML PATH (''))),' ',';'),
       ProductNumber, SUM(ProductQuanity) AS Quantity
FROM myTable T
GROUP BY ProductNumber
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900