Pivot to obtain EAV data

Posted by Snowy on Stack Overflow See other posts from Stack Overflow or by Snowy
Published on 2011-09-09T18:12:42Z Indexed on 2012/10/13 15:38 UTC
Read the original article Hit count: 157

I have an EAV table (simple key/value in every row) and I need to take the 'value' from two of the rows and concat them into a single row with a single column. I can't seem to get through the part where I just have the pivot straight. Can anyone help me figure this out?

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'

That makes:

Key      hmm1     hmm2
-------- -------- --------
key1     NULL     NULL
key2     NULL     NULL

But what I want to make is:

hmmmX
-----
aaa;bbb

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about sql-server-2008