4

我查看了许多在 SQL Server 中模拟“组连接”功能的解决方案。不过,我想制作一个更具人类可读性的解决方案,但我不知道该怎么做。

我有一个看法:

ParentID | ChildName

其中包含记录,例如:

1 | Max
1 | Jessie
2 | Steven
2 | Lucy
2 | Jake
3 | Mark

我想“组合 Concat”这些以获得:

1 | Max and Jessie
2 | Steven, Lucy and Jake
3 | Mark

因此,如果只有 1 个孩子,则只返回名称,如果有多个,则将最后 2 个用 ' 和 ' 连接,其他所有用 ', ' 连接。

我有点坚持如何在不求助于 CLR 的情况下做到这一点,我不想这样做。我对一个功能很满意 - 但速度是一个问题,我如何确定子编号以便我可以在 ' 和 '、'、' 或 '' 之间进行选择?

4

3 回答 3

4

制作更人性化的解决方案

对不起,这是我能满足您的要求的最好方法。

SQL小提琴

MS SQL Server 2008 架构设置

create table YourTable
(
  ParentID int,
  ChildName varchar(10)
);

insert into YourTable values
(1, 'Max'),
(1, 'Jessie'),
(2, 'Steven'),
(2, 'Lucy'),
(2, 'Jake'),
(3, 'Mark');

查询 1

with T as 
(
  select ParentID,
         ChildName,
         row_number() over(partition by ParentID order by ChildName) as rn,
         count(*) over(partition by ParentID) as cc
  from YourTable
)
select T1.ParentID,
       (
         select case
                  when T2.rn = 1 and T2.cc > 1 then ' and '
                  else ', ' 
                end + T2.ChildName
         from T as T2
         where T1.ParentID = T2.ParentID
         order by T2.rn desc
         for xml path(''), type
       ).value('substring(text()[1], 3)', 'varchar(max)') as ChildNames
from T as T1
group by T1.ParentID

结果

| PARENTID |            CHILDNAMES |
------------------------------------
|        1 |        Max and Jessie |
|        2 | Steven, Lucy and Jake |
|        3 |                  Mark |
于 2012-11-23T06:32:38.243 回答
1
select ParentID,STUFF((SELECT ' and '+ChildName
    FROM Table1 where ParentID=a.ParentID
     FOR XML PATH('')),1,4,'')  as cnmae from Table1 a
group by ParentID

SQL 小提琴演示

于 2012-11-23T06:10:43.460 回答
1

很好的逻辑问题。请检查下面的查询(有点冗长,但无法阻止自己发布我的小逻辑:))。

CREATE TABLE #SampleTable ([ParentID] int, [ChildName] varchar(6));

INSERT INTO #SampleTable VALUES (1, 'Max')
INSERT INTO #SampleTable VALUES (1, 'Jessie')
INSERT INTO #SampleTable VALUES (2, 'Steven')
INSERT INTO #SampleTable VALUES (2, 'Lucy')
INSERT INTO #SampleTable VALUES (2, 'Jake')
INSERT INTO #SampleTable VALUES (3, 'Mark')

select * From #SampleTable

;WITH T(xParentID, xChildName, xChildNameResult, xC1, xC2)AS
(
    SELECT * FROM(
    SELECT 
        ParentID ,
        ChildName, 
        CAST(ChildName AS NVARCHAR(MAX)) AS ChildNameResult,
        ROW_NUMBER() OVER (PARTITION BY [ParentID] ORDER BY ChildName) C1,
        COUNT(*) OVER (PARTITION BY [ParentID]) C2
    FROM #SampleTable)x WHERE x.C1=1

    UNION ALL

    SELECT ParentID, ChildName,
    CAST(T.xChildNameResult+(CASE WHEN C1=1 THEN '' WHEN C1=C2 THEN ' and ' ELSE ', ' END)+ChildName AS NVARCHAR(MAX)), C1, C2 
    FROM
    (
        SELECT 
        ParentID , 
            ChildName, 
            ROW_NUMBER() OVER (PARTITION BY ParentID order by ChildName) C1,
            COUNT(*) OVER (PARTITION BY ParentID) C2
        FROM #SampleTable
    )y INNER JOIN T ON y.ParentID=T.xParentID and y.c1=T.xC1+1
)SELECT xParentID, xChildNameResult FROM T where xC1=xC2
OPTION (MAXRECURSION 0);
于 2012-11-23T06:38:51.430 回答