我有两张桌子。其中一个条目如下:
User_ID Cat_ID
1 [1,2]
2 [1]
3 [3]
另一方面,我有与类别ID相关联的字符串:
Cat_ID Cat_Name
1 Category1
2 Category2
3 Category3
我希望能够在CatID上执行连接,所以我得到了类似的内容:
User_ID Category_ID
1 ['Category1', 'Category2']
2 ['Category1']
3 ['Category3']
我理解列上的连接,如果该列只有单个条目以允许一对一映射,但是如何为同一列执行多对一映射?
答案 0 :(得分:0)
为了将值拆分为列,我们需要创建函数
create function [dbo].[udf_splitstring] (@tokens varchar(max),
@delimiter varchar(5))
returns @split table (
token varchar(200) not null )
as
begin
declare @list xml
select @list = cast('<a>'
+ replace(@tokens, @delimiter, '</a><a>')
+ '</a>' as xml)
insert into @split
(token)
select ltrim(t.value('.', 'varchar(200)')) as data
from @list.nodes('/a') as x(t)
return
end
create table #a
(
User_ID int, Cat_ID varchar(10)
)
insert into #a values
(1,'1,2'),
(2,'1'),
(3,'3')
create table #b
(
Cat_ID int, Cat_Name varchar(50)
)
insert into #b values
(1,'Category1'),
(2,'Category2'),
(3,'Category3')
;WITH CTE AS
(
SELECT USER_ID,B.CAT_NAME FROM (SELECT T.USER_ID,CAST(X.TOKEN AS INT) AS CAT_ID
FROM #A T
CROSS APPLY (SELECT TOKEN FROM [UDF_SPLITSTRING](T.CAT_ID,',') ) X)C
JOIN (SELECT * FROM #B) B ON B.CAT_ID=C.CAT_ID
)
SELECT USER_ID
,STUFF((SELECT ', ' + CAST(CAT_NAME AS VARCHAR(10)) [text()]
FROM cte
WHERE USER_ID = t.USER_ID
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,2,' ') Category_ID
FROM cte t
GROUP BY USER_ID
USER_ID Category_ID
1 Category1, Category2
2 Category1
3 Category3