I am trying to get from this
tPeople
Name
Alice
Bob
tAnimals
Name Animal
Alice Cat
Alice Cat
Bob Horse
Bob Dog
To this
Alice "Cat*2"
Bob "Horse, Dog"
So far I have an intermediate table grouping and counting animals
tGrouped
Name Animal Tally
Alice Cat 2
Bob Dog 1
Bob Horse 1
And if I run
SELECT Name, Animals=STUFF((
SELECT N', '+animal+'*'+tally FROM tgrouped as g
WHERE g.Name = p.Name
FOR XML PATH(''), TYPE).value(N'.[1]', N'varchar(max)'), 1, 2, N'')
FROM tpeople as p
I get
Name Animals
Alice Cat*2
Bob Dog*1, Horse*1
Is there any way to skip the intermediate table? And any way to not show the tally when it is 1?
"Cat*2"but not"Dog*1"? Are there other times you would have different notations? - Larnu