使用MS SQL Server 2005,如何将详细记录合并到一个逗号分隔列表中

时间:2021-11-30 00:19:49

BACKGROUND:**I am running **MS2005. I have a MASTER table (ID, MDESC) and a DETAIL table (MID, DID, DDESC) with data as follows

背景:**我正在运行** MS2005。我有一个MASTER表(ID,MDESC)和一个DETAIL表(MID,DID,DDESC),数据如下

1 MASTER_1
2 MASTER_2
1 L1 DETAIL_M1_L1
1 L2 DETAIL_M1_L2
1 L3 DETAIL_M1_L3
2 L1 DETAIL_M2_L1
2 L2 DETAIL_M2_L2

If I join the tables with

如果我加入表格

SELECT M.*, D.DID FROM MASTER M INNER JOIN DETAIL D on M.ID = D.MID

I get a list like the following:

我得到如下列表:

1 MASTER_1 L1
1 MASTER_1 L2
1 MASTER_1 L3
2 MASTER_2 L1
2 MASTER_2 L2

QUESTION: Is there any way to use a MS SQL select statement to get the detail records into a comma separated list like this:

问题:有没有办法使用MS SQL select语句将详细记录获取到逗号分隔列表中,如下所示:

1 MASTER_1 "L1, L2, L3"
2 MASTER_2 "L1, L2"

5 个解决方案

#1


You need a function:-

你需要一个功能: -

 CREATE FUNCTION [dbo].[FN_DETAIL_LIST]
 (
     @masterid int
 )
 RETURNS varchar(8000)
 AS 
 BEGIN
     DECLARE @dids varchar(8000)

     SELECT @dids = COALESCE(@dids + ', ', '') + DID
     FROM DETAIL
     WHERE MID = @masterid
     RETURN @dids
 END

Usage:-

SELECT MASTERID, [dbo].[FN_DETAIL_LIST](MASTERID) [DIDS]
FROM MASTER

#2


Thanks to the concept in the link from Bill Karwin, it's the CROSS APPLY that makes it work

感谢Bill Karwin链接中的概念,它是CROSS APPLY,它使它工作

SELECT ID, DES, LEFT(DIDS, LEN(DIDS)-1) AS DIDS
 FROM MASTER M1 INNER JOIN DETAIL D on M1.ID = D.MID 
  CROSS APPLY (
    SELECT DID + ', '
    FROM MASTER M2 INNER JOIN DETAIL D on M2.ID = D.MID 
    WHERE M1.ID = M2.ID
    FOR XML PATH('')
   ) pre_trimmed (DIDS)
GROUP BY ID, DES, DIDS

RESULTS:

ID  DES        DIDS
--- ---------- ---------------
1   MASTER_1   L1, L2, L3
2   MASTER_2   L1, L2

#3


coalesce is your friend.

coalesce是你的朋友。

declare @CSL vachar(max)

set @CSL = NULL
select @CSL = coalesce(@CSL + ', ', '') + cast(DID as varchar(8))
from MASTER M INNER JOIN DETAIL D on M.ID = D.MID

select @CSL

This will not work well for a generalized query (i.e. works great for a single master record).

这对于通用查询不适用(即对单个主记录很有效)。

You could drop this into a function... but that may not give you the performance you need/want.

您可以将其放入函数中......但这可能无法满足您所需/想要的性能。

#4


This is the purpose of MySQL's GROUP_CONCAT() aggregate function. Unfortunately, it's not very easy to duplicate this function in other RDBMS brands that don't support it.

这是MySQL的GROUP_CONCAT()聚合函数的目的。不幸的是,在不支持它的其他RDBMS品牌中复制此功能并不是一件容易的事。

See Simulating group_concat MySQL function in Microsoft SQL Server 2005?

请参阅在Microsoft SQL Server 2005中模拟group_concat MySQL函数?

#5


I think you need a function for this to work properly in recent version of SQL Server:

我认为您需要一个函数才能在最新版本的SQL Server中正常工作:

http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true

#1


You need a function:-

你需要一个功能: -

 CREATE FUNCTION [dbo].[FN_DETAIL_LIST]
 (
     @masterid int
 )
 RETURNS varchar(8000)
 AS 
 BEGIN
     DECLARE @dids varchar(8000)

     SELECT @dids = COALESCE(@dids + ', ', '') + DID
     FROM DETAIL
     WHERE MID = @masterid
     RETURN @dids
 END

Usage:-

SELECT MASTERID, [dbo].[FN_DETAIL_LIST](MASTERID) [DIDS]
FROM MASTER

#2


Thanks to the concept in the link from Bill Karwin, it's the CROSS APPLY that makes it work

感谢Bill Karwin链接中的概念,它是CROSS APPLY,它使它工作

SELECT ID, DES, LEFT(DIDS, LEN(DIDS)-1) AS DIDS
 FROM MASTER M1 INNER JOIN DETAIL D on M1.ID = D.MID 
  CROSS APPLY (
    SELECT DID + ', '
    FROM MASTER M2 INNER JOIN DETAIL D on M2.ID = D.MID 
    WHERE M1.ID = M2.ID
    FOR XML PATH('')
   ) pre_trimmed (DIDS)
GROUP BY ID, DES, DIDS

RESULTS:

ID  DES        DIDS
--- ---------- ---------------
1   MASTER_1   L1, L2, L3
2   MASTER_2   L1, L2

#3


coalesce is your friend.

coalesce是你的朋友。

declare @CSL vachar(max)

set @CSL = NULL
select @CSL = coalesce(@CSL + ', ', '') + cast(DID as varchar(8))
from MASTER M INNER JOIN DETAIL D on M.ID = D.MID

select @CSL

This will not work well for a generalized query (i.e. works great for a single master record).

这对于通用查询不适用(即对单个主记录很有效)。

You could drop this into a function... but that may not give you the performance you need/want.

您可以将其放入函数中......但这可能无法满足您所需/想要的性能。

#4


This is the purpose of MySQL's GROUP_CONCAT() aggregate function. Unfortunately, it's not very easy to duplicate this function in other RDBMS brands that don't support it.

这是MySQL的GROUP_CONCAT()聚合函数的目的。不幸的是,在不支持它的其他RDBMS品牌中复制此功能并不是一件容易的事。

See Simulating group_concat MySQL function in Microsoft SQL Server 2005?

请参阅在Microsoft SQL Server 2005中模拟group_concat MySQL函数?

#5


I think you need a function for this to work properly in recent version of SQL Server:

我认为您需要一个函数才能在最新版本的SQL Server中正常工作:

http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true