ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 254.7k

How to add feature value without prevent group data based on

Feb 3 2020 9:33 PM
How to add feature value without prevent group data based on itemid pivot table?
 
I work on SQL server 2012
I make pivot table based on itemId
it work good but after add featurevalue data repeated
and not grouping
How to add Featurevalue without prevent repeated data on pivot table ?
desired result
  1. ItemCode  IPN    PartnerName CustomerName   Fan Refrigator  temprature  FeatureValue  
  2. 1     1233   Saico        Michel        1        2                 1            1234            
  3. 2     5433   Mbaby        Michel        0        1                 0            7777  
  4. 3     44333  sadeoMany    Michel        1        0                 1            88888  
  1. What I have tried:  
  2.   
  3. create table #InputData  
  4. (  
  5. CustomerID uniqueidentifier  
  6.   
  7. )  
  8. insert into #InputData values ('0ce19920-f0ca-433c-abb1-4e84d52b618b')  
  9.   
  10. create table #customers  
  11. (  
  12. CustomerID uniqueidentifier,  
  13. CustomerName  nvarchar(200)  
  14.   
  15. )  
  16. insert into #customers   
  17. values  
  18. ('0ce19920-f0ca-433c-abb1-4e84d52b618b','Michel'),  
  19. ('188b8053-18c0-4092-955e-962f54485e43','Jakson')  
  20.   
  21. create table #FeatureType  
  22. (  
  23. FeatureId int,  
  24. FeatureName  nvarchar(200)  
  25.   
  26. )  
  27. insert into #FeatureType   
  28. values  
  29. (1,'temprature'),  
  30. (2,'Fan'),  
  31. (3,'Refrigator')  
  32.   
  33. create table #Items  
  34. (  
  35. ItemId int,  
  36. IPN  nvarchar(200),  
  37. PartnerPart  nvarchar(200),  
  38. PartnerName nvarchar(100)  
  39. )  
  40. insert into #Items   
  41. values  
  42. (1,'1233','Mobilic','Saico'),  
  43. (2,'5433','Saldom','Mbaby'),  
  44. (3,'44333','Silicon','sadeoMany')  
  45.   
  46. create table #ItemFeatures  
  47. (  
  48. ItemFeatureId int,  
  49. ItemId  int,  
  50. FeatureId int,  
  51. CustomerId uniqueidentifier,  
  52. FeatureValue  nvarchar(50)  
  53. )  
  54. insert into #ItemFeatures   
  55. values  
  56. (1,1,1,'0ce19920-f0ca-433c-abb1-4e84d52b618b','1234'),  
  57. (2,1,2,'0ce19920-f0ca-433c-abb1-4e84d52b618b','4333'),  
  58. (3,1,3,'0ce19920-f0ca-433c-abb1-4e84d52b618b','55555'),  
  59. (4,1,3,'0ce19920-f0ca-433c-abb1-4e84d52b618b','66666'),  
  60. (5,2,3,'0ce19920-f0ca-433c-abb1-4e84d52b618b','7777'),  
  61. (6,3,1,'0ce19920-f0ca-433c-abb1-4e84d52b618b','88888'),  
  62. (7,3,2,'0ce19920-f0ca-433c-abb1-4e84d52b618b','99999')  
  63.   
  64. DECLARE @Columns as VARCHAR(MAX)  
  65. SELECT @Columns =  
  66. COALESCE(@Columns + ', ','') + QUOTENAME(FeatureName)  
  67. FROM  
  68. --distinct FT.FeatureName   
  69. (select  distinct FT.FeatureName  from #InputData Feat inner join #ItemFeatures ItemF  
  70. on ItemF.CustomerId=Feat.CustomerId INNER join #FeatureType FT on ItemF.FeatureId=FT.FeatureId  
  71.   
  72.    ) AS B  
  73.    ORDER BY B.FeatureName  
  74.   
  75. DECLARE @SQLs as VARCHAR(MAX)  
  76.   
  77. SET @SQLs = 'SELECT ItemCode, IPN,PartnerName,CustomerName,FeatureValue ' + @Columns + '  
  78. FROM  
  79. (  
  80.   select F.ItemId,F.ItemId as ItemCode,I.IPN,I.PartnerName,I.PartnerPart,c.CustomerName,t.FeatureName,F.FeatureValue  from #InputData Itm   
  81.  inner join #ItemFeatures F on F.CustomerId=Itm.CustomerId    
  82.  inner join #Items I on I.ItemID=F.ItemId  
  83.  inner join #FeatureType T on T.FeatureId=F.FeatureId   
  84.  inner join #customers c on c.CustomerID=F.CustomerID   
  85. as PivotData  
  86. PIVOT  
  87. (  
  88.    COUNT(ItemId)  
  89.    FOR FeatureName IN (' + @Columns + ')  
  90. AS PivotResult  
  91. ORDER BY CustomerName'  
  92.   
  93. EXEC(@SQLs)