Solved

Concatenate ProductName and SubProductName separated by comma

Posted on 2011-02-17
6
556 Views
Last Modified: 2012-05-11
Hello Experts,

I am not completely new to sql but not familiar with everything as well. I am stumbled with one problem which I thought was easy :)

Anyways, here is the senario:
I have two tables named Products and SubProducts. Each product is related to some SubProducts. What I am trying to accomplish is return ProductName and SubProductName in following manner.

ProductNameOne|SubProductNameOne|SubProductNameTwo|SubProductNameThree

ProductNameTwo|SubProductNameOne|SubProductNameTwo|SubProductNameThree

ProductNameThree|SubProductNameOne|SubProductNameTwo|SubProductNameThree  

.......hope you get the idea. so concatenate ProductName and SubProductNames that are related to that ProductName.

So row one would contain concatenation of first ProductName and it's related SubProductNames
Rows two would contain concatenation of second ProductName and it's related SubProductNames....

Any ideas/suggestions are highly welcome.  

Thank you in advance for all you guys help :)
0
Comment
Question by:KoolAsIce
[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
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 34919794
This will do it for you:
Replace anything in the <>

SELECT cast(p.<PARENTCOL>as varchar(50)) + ',' + el.values
FROM <PARENT TABLE> p
CROSS APPLY (SELECT cast(<CHILDCOL> as varchar(20)) + ',' AS [text()]
             FROM <CHILDTABLE>
             WHERE <PARENTCOL_ID = ChildTable_ParentCOL ID (FK>
             FOR XML PATH(''))el(values)


0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 34919799
forgot values was a keyword.  below will work.

SELECT cast(p.<PARENTCOL>as varchar(50)) + ',' + el.MYvalues
FROM <PARENT TABLE> p
CROSS APPLY (SELECT cast(<CHILDCOL> as varchar(20)) + ',' AS [text()]
             FROM <CHILDTABLE>
             WHERE <PARENTCOL_ID = ChildTable_ParentCOL ID (FK>
             FOR XML PATH(''))el(MYvalues)
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 34919830
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:KoolAsIce
ID: 34920081
You are genius! I was trying to do same thing using three temporary tables and almost 100 lines of code!

Thank you so much for your help!
0
 

Author Comment

by:KoolAsIce
ID: 34947447
Can someone tell me how to avoid '&' displaying as &amp;

In the attached code, the column SubCategoryName has a name containing '&' and result is displayed as '&amp;'

I assume it's because of the use of FOR XML PATH(' ').

All the help would be greatly appreciated on how to avoid that.  
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

628 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