0

products

-----------------------------
| ProductId | ModelId       |
|---------------------------|
| 12345     | A3666         |
| 12345     | A3667         |
| 12345     | A8999         |
| 12346     | A3666         |
| 12346     | A3667         |
-----------------------------

models

-----------------------------
| ModelId   | Name          |
|---------------------------|
| A3666     | win           |
| A3667     | xia           |
| A8999     | vor           |
-----------------------------

I'm trying to get the output this way:

-----------------------------------
| ProductId | Models              |
|---------------------------------|
| 12345     | win,xia,vor         |
| 12346     | win,xia             |
-----------------------------------

My code is :

SELECT
    p.ProductId,
    STUFF
    (
        (
            SELECT ',' + Name
            FROM models m
            WHERE m.ModelId=p.ModelId
            ORDER BY Name
            FOR XML PATH(''), type
        ).value('.', 'varchar(max)'), 1, 1, ''
    ) AS ModelNames
FROM
    products p

which gives the o/p :

-----------------------------
| ProductId | ModelNames    |
|---------------------------|
| 12345     | win           |
| 12345     | xia           |
| 12345     | vor           |
| 12346     | win           |
| 12346     | xia           |
-----------------------------

where am i going wrong. [I think the question was clear enough to be understood but SO wouldnt submit as it says, add more details, mostly code. hence this text.]

3
  • @Querty What is the error that you are facing. Commented Nov 5, 2014 at 6:44
  • No error. Just not the desired o/p, if you see.. Commented Nov 5, 2014 at 6:46
  • 1
    I don't know why people are so obsessed with using CSV to transfer data in and out of SQL Server. SQL Server has at least two data types (tables and xml) that are designed to hold multiple values. Unlike strings for which you have to write all kinds of odd code to either construct them (as here) or rip them apart to get the values back out. And lose any data type checking that you might have usefully obtained. Commented Nov 5, 2014 at 7:16

4 Answers 4

1

I think this should solve your issue.

SELECT DISTINCT
    p.ProductId,
    STUFF
    (
        (
            SELECT ',' + m.Name
            FROM models m
            INNER JOIN products ip
            ON m.ModelId = ip.ModelId
            WHERE ip.ProductId = p.ProductId
            ORDER BY p.ModelId
            FOR XML PATH('')
        ), 1, 1, ''
    ) AS ModelNames
FROM products p
Sign up to request clarification or add additional context in comments.

2 Comments

it duplicates the comma separated values - eg 12345 - win,xia,vor,win,xia,vor
I tested your given setup on my SQL Server 2014 and things worked like expected. Is your real setup working slightly different?
1

Try this:

SELECT DISTINCT
    p.ProductId,
    STUFF
    (
        (
            SELECT ',' + Name
            FROM models m
            WHERE m.ModelId IN (SELECT ModelId FROM products WHERE ProductId = p.ProductId)
            ORDER BY Name
            FOR XML PATH(''), type
        ).value('.', 'varchar(max)'), 1, 1, ''
    ) AS ModelNames
FROM
    products p

2 Comments

Its still the same, as the o/p i was geeting
@Qwerty I have tested my code in SSMS and it gives me these records as output: (12345 vor,win,xia), (12346 win,xia). I really can't think what could be going wrong with your configuration.
1

try with this, this matches the output you are looking for

DECLARE @ProductModel AS Table(ProductId  INT,ModelId Varchar(10))

INSERT INTO @ProductModel
VALUES(12345,'A3666'), 
(12345,'A3667'),      
(12345,'A8999'),
(12346,'A3666'),
(12346,'A3667')

DECLARE @Model AS Table(ModelId  Varchar(10),Name Varchar(10))

Insert into @Model Values('A3666','win'),('A3667','xia'),('A8999','vor')          


SELECT
    p2.ProductId,
    STUFF
    (
        (
            SELECT ',' + Name
            FROM @ProductModel p1
            INNER JOIN @Model m ON m.ModelId=p1.ModelId
            WHERE p1.ProductId=p2.ProductId           
            ORDER BY Name
            FOR XML PATH(''), type
        ).value('.', 'varchar(max)'), 1, 1, ''
    ) AS ModelNames
FROM
    @ProductModel p2
    GROUP BY p2.ProductId

Comments

0

Please Replace this code with your stuff. I am really guessing this answer.

SELECT
    Distinct
    p.ProductId,
    coalesce(models + ', ', '')
FROM
    products p
    --Other joins----

1 Comment

did not work. it gives all names for each product now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.