我有一个EAV表(每行中都有简单的键/值),我需要从两行中取出'value'并将它们连接成一行,只有一列。我似乎无法通过直接转向的部分。任何人都可以帮我解决这个问题吗?
Declare @eavHelp Table
(
[Key] VARCHAR (8) NOT NULL,
[Value] VARCHAR (8) NULL
)
Insert Into @eavHelp Values ( 'key1' , 'aaa' )
Insert Into @eavHelp Values ( 'key2' , 'bbb' )
Select * From @eavHelp
Pivot
( Min( [Value] )
For [Value] in ( hmm1 , hmm2 )
)
as Piv Where [Key] = 'key1' or [Key] = 'key2'
这样做:
Key hmm1 hmm2
-------- -------- --------
key1 NULL NULL
key2 NULL NULL
但我想做的是:
hmmmX
-----
aaa;bbb
答案 0 :(得分:4)
不需要转动。
编辑添加新要求(;分隔符):
SELECT hmmmX = STUFF((SELECT ';' + [Value] FROM @eavHelp
WHERE [Key] IN ('key1', 'key2')
FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 1, '');
答案 1 :(得分:1)
Declare @eavHelp Table
(
[Key] VARCHAR (8) NOT NULL,
[Value] VARCHAR (8) NULL
)
Insert Into @eavHelp Values ( 'key1' , 'aaa' )
Insert Into @eavHelp Values ( 'key2' , 'bbb' )
declare @concat nvarchar(max); select @concat =''
select @concat =@concat+case when len(@concat)>0 then '; ' else '' end + value from @eavHelp
select @concat