I designed an EAV table that looks like this:
SID AID VID
1 1 1
1 2 1
1 3 2
1 4 3
1 1 2
SID stand for Subject ID, AID stands for Attribute ID and VID stands for ValuedID
also a table to map the attributes:
AttributeID AttributeName
1 Hobbies
2 Name
3 Gender
4 IrisColor
After using pivot on the first table, linked to the attribute table:
SELECT
SubjectID,
Hobbies,
Name,
Gender,
IrisColor
FROM
(
SELECT SubjectID, attr.AttributeName as attribute, ValueID from SubjectDetails, SubjectAttributes as attr WHERE SubjectDetails.AttributeID=attr.ID
) as t
PIVOT(
MAX(ValueID)
FOR attribute IN (Hobbies,Name,Gender,IrisColor)) AS t1
WHERE SubjectID=1
I get this:
SubjectID Hobbies Name Gender IrisColor
1 1 1 2 3
Which is almost correct, but SubjectAttribute 1 (which is hobbies) appears one more time in the first table (SubjectDetails), so what I want to achieve is this:
SubjectID Hobbies Name Gender IrisColor
1 **1,2** 1 2 3
I have to mention that I don't care about what separator is used and that I tried doing that with the STUFF function but it is a pain to combine PIVOT and STUFF (or I just don't know how).. Any suggestions?