SQLTeam.com | Weblogs | Forums

How to get feature key and feature value separated $ based on part id?

I work on sql server 2012 i need to get featurekey and feature value separated $
Based on partid but i don't know how o do that by select sql query ?
expected result as below

PartId Featurekey FeatureValue
1550 Botato$Mango$dates Yellow$Red$Black
1600 Rice$macrona$chicken white$Red$Yellow
1700 Guava$grapes$FIG Yellow$Green$Red
sample data

 create table #PartsFeature
 (
 PartId int,
 Featurekey nvarchar(200),
 FeatureValue nvarchar(200),
 )
 insert into #PartsFeature(PartId,Featurekey,FeatureValue)
 values
 (1550,'Botato','Yellow'),
 (1550,'Mango','Red'),
 (1550,'dates','Black'),
 (1600,'Rice','white'),
 (1600,'macrona','Red'),
 (1600,'chicken','Yellow'),
 (1700,'Guava','Yellow'),
 (1700,'grapes','Green'),
 (1700,'FIG','Red')

what have you tried? Read up concat

hope this helps .. if i remember correctly you have 2008 where this will not work

select 
   PartId , 
   STRING_AGG(Featurekey, ', ') AS Featurekey,
   STRING_AGG(FeatureValue, ', ') AS FeatureValue
FROM 
   #PartsFeature
GROUP BY 
   PartId;

image