获取逗号分隔的值列表 - SQL

时间:2014-11-05 06:41:31

标签: sql tsql sql-server-2012

产品

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

模型

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

我试图以这种方式获得输出:

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

我的代码是:

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

给出了o / p:

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

我哪里出错了。 [我认为这个问题很明显可以理解,但是如果说它不会提交,请添加更多细节,主要是代码。因此,本文。]

4 个答案:

答案 0 :(得分:1)

我认为这可以解决您的问题。

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

答案 1 :(得分:1)

试试这个:

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 :(得分:1)

试试这个,这匹配你要找的输出

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

答案 3 :(得分:0)

请用您的资料替换此代码。 我真的在猜这个答案。

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