结合多行与COALESCE

时间:2018-11-20 11:14:58

标签: sql sql-server-2012 coalesce

使用SQL Server 2012

我有下表:

Id      Description 
6192    Salzburg
6193    Salzburg
6194    Salzburg
6196    Innsbruck
6197    Innsbruck
6198    Innsbruck
6199    Innsbruck
6201    Bregenz
6202    Bregenz
6203    Bregenz

我想在一个字符串中选择所有ID一起选择每个不同的“说明”:

    Description     Ids
    Salzburg        '6192,6193,6194'
    Innsbruck       '6196,6197,6198'

我在此网站[How to concatenate text from multiple rows into a single text string in SQL server?上看到了一些类似的代码,但出于我的目的(我不想使用XML路径!),我仍无法弄清楚。到目前为止,这是我尝试过的:

DECLARE @ids AS Nvarchar(MAX)
SELECT  @ids = COALESCE(@ids + ',', '') + CAST(t.Id AS nvarchar(5))
    FROM (SELECT tmp.Id FROM (SELECT id, [Description] FROM tblMasterPropValues WHERE IdCategory = 253 AND IsActive = 1) as tmp
        WHERE [Description] = tmp.[Description]) AS t
SELECT @ids
--SELECT DISTINCT [Description], @ids AS IDs FROM tblMasterPropValues WHERE IdCategory = 253 AND IsActive = 1 AND Id IN (@ids)

我实在无法解决问题,并且希望获得任何帮助。

4 个答案:

答案 0 :(得分:1)

您可以尝试使用STUFF()函数

SELECT description,  Ids = STUFF(
             (SELECT ',' + Id
              FROM tblMasterPropValues t1
              WHERE t1.description = t2.description
              FOR XML PATH (''))
             , 1, 1, '') from tblMasterPropValues t2
group by description;

答案 1 :(得分:1)

因为FOR XML PATH()是正确的子句,所以,您可以执行以下操作:

SELECT DISTINCT v.description, STUFF(v1.ids, 1, 1, '''') + '''' 
FROM tblMasterPropValues v CROSS APPLY
     (SELECT ', '+ CAST(v1.Id AS VARCHAR(255))
      FROM tblMasterPropValues v1
      WHERE v1.description = v.description 
      FOR XML PATH('')
     ) v1(ids);

答案 2 :(得分:0)

尝试一下:

DECLARE @Table TABLE(ID INT, Description VARCHAR(25))
INSERT INTO @Table
VALUES (6192,'Salzburg' )
,(6193,'Salzburg' )
,(6194,'Salzburg' )
,(6196,'Innsbruck')
,(6197,'Innsbruck')
,(6198,'Innsbruck')
,(6199,'Innsbruck')
,(6201,'Bregenz'     )
,(6202,'Bregenz'     )
,(6203,'Bregenz'     )

查询:

SELECT DISTINCT T2.Description, 
    SUBSTRING(
        (
            SELECT ','+CAST(T1.ID AS VARCHAR)  AS [text()]
            FROM @Table T1
            WHERE T1.Description = T2.Description
            ORDER BY T1.Description
            FOR XML PATH ('')
        ), 2, 1000) [Ids]
FROM @Table T2

结果:

Description Ids
Bregenz     6201,6202,6203
Innsbruck   6196,6197,6198,6199
Salzburg    6192,6193,6194

答案 3 :(得分:0)

您还可以使用递归CTE来实现

DECLARE @tblMasterPropValues TABLE (Id INT, Description VARCHAR(20))
INSERT INTO @tblMasterPropValues VALUES
(6192 , 'Salzburg'),
(6193 , 'Salzburg'),
(6194 , 'Salzburg'),
(6196 , 'Innsbruck'),
(6197 , 'Innsbruck'),
(6198 , 'Innsbruck'),
(6199 , 'Innsbruck'),
(6201 , 'Bregenz'),
(6202 , 'Bregenz'),
(6203 , 'Bregenz')



;WITH Tbl AS 
(
    SELECT 
        *, 
        ROW_NUMBER() OVER(PARTITION BY Description ORDER BY Id) AS RN,
        COUNT(*) OVER(PARTITION BY Description) AS CNT
    FROM @tblMasterPropValues 
)
, Rcr AS (
    SELECT *, CAST(Id AS varchar(max)) Ids 
    FROM Tbl WHERE RN = 1
        UNION ALL
    SELECT T.*, Rcr.Ids + ',' + CAST(T.Id AS VARCHAR(10)) Ids 
    FROM Rcr 
        INNER JOIN Tbl T ON T.RN = Rcr.RN + 1 and Rcr.Description = T.Description
)
SELECT RN, Description, Ids FROM Rcr 
WHERE RN = CNT

结果:

Description          Ids
-------------------- -----------------------
Salzburg             6192,6193,6194
Innsbruck            6196,6197,6198,6199
Bregenz              6201,6202,6203