ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 254k

How to display size feature on final result where partc and partx not

Feb 8 2021 6:20 AM
How to display size feature on final result where partc and partx not have same feature ? 

I work on SQL server 2012 I face issue i can't display feature size on final result
query
this happen when replace temp table partc and part x not have value to same feature as size feature
but if part c and partx have same feature no problem it is ok

as example below :

  1. DROP TABLE IF EXISTS #replace  
  2.  DROP TABLE IF EXISTS #FeatureNameandValues  
  3.  DROP TABLE IF EXISTS #Temp  
  4.  DROP TABLE IF EXISTS #Temp1  
  5.  DROP TABLE IF EXISTS #Temp2  
  6.       
  7.  create table #replace  
  8.   (  
  9.   PartIdc int,  
  10.   PartIdx int,  
  11.   )  
  12.   insert into #replace(PartIdc,PartIdx)  
  13.   values  
  14.   (1211,1300),  
  15.   (2000,2200),  
  16.   (3000,3100),  
  17.   (4150,4200)  
  18.           
  19.   create table #FeatureNameandValues  
  20.   (  
  21.   PartId int,  
  22.   [FeatureName] nvarchar(20),  
  23.   [FeatureValue] int  
  24.   )  
  25.   insert into #FeatureNameandValues(PartId,[FeatureName],[FeatureValue])  
  26.   values  
  27.   (1211,'Weight',5),  
  28.   (2000,'Tall',20),  
  29.   (3000,'Weight',70),  
  30.   (4150,'Tall',190),  
  31.   (1211,'Tall',80),  
  32.   (1300,'Weight',10),  
  33.   (3100,'Size',150),  
  34.   (4200,'Tall',130),  
  35.   (1300,'Tall',20)  
  36.       
  37.       
  38.       
  39.       
  40.       
  41.   SELECT a.[FeatureName] [FeatureName], CASE WHEN a.PartId = b.PartIdc THEN 1 WHEN a.PartId=b.PartIdx THEN 2 END PartOrder, b.PartIdc PartC,b.PartIdx PartX,  a.[FeatureValue] [FeatureValue]  
  42.   INTO #Temp  
  43.   FROM #FeatureNameandValues a  
  44.   JOIN #replace b ON  a.PartId = b.PartIdc OR a.PartId = b.PartIdx  
  45.       
  46.   -- Find out different values   
  47.   -- If value belongs to PartC, then order = 1; PartX, order = 2   
  48.   -- So that the feature value for c will be the former one  
  49.   SELECT a.[FeatureName] [FeatureName], a.PartOrder, a.PartC PartC, a.PartX PartX, a.[FeatureValue] [FeatureValue]  
  50.   INTO #Temp1  
  51.   FROM #Temp a  
  52.   JOIN #Temp b ON a.FeatureName=b.FeatureName AND a.PartC=b.PartC AND a.PartX=b.PartX AND a.[FeatureValue] <> b.[FeatureValue]  
  53.       
  54.   -- Display the result for different values  
  55.   SELECT * FROM #Temp1   
  56.   ORDER BY  PartC,PartX,[FeatureName],PartOrder  
  57.       
  58.       
  59.   -- Concatenate the values for each group  
  60.  SELECT T1.[FeatureName], T1.PartC, T1.PartX,  
  61.          STUFF(    
  62.          (    
  63.          SELECT '-' + CAST(T2.[FeatureValue] AS VARCHAR(MAX))  
  64.          FROM #Temp1 T2    
  65.          WHERE T1.[FeatureName] = T2.[FeatureName] AND T1.PartC = T2.PartC AND T1.PartX = T2.PartX  
  66.          FOR XML PATH ('')    
  67.          ),1,1,'') [Difference]  
  68.  INTO #Temp2   
  69.  FROM #Temp1 T1    
  70.  GROUP BY  T1.PartC,T1.PartX,T1.[FeatureName]  
  71.       
  72.  SELECT * FROM #Temp2  
  73.       
  74.  -- Out one row  
  75.  SELECT STUFF(  
  76.  (SELECT ' | ' + [FeatureName] + '( '+ [Difference] + ' )' FROM #Temp2 FOR XML PATH('')),  
  77.  1,2,''AS [Result]  
 
  1. final result expected is :  
  2.   
  3.  Tall (80-20) | Weight(5-10) | size(NULL-150) | Tall(190-130)  
  1. wrong result is  
  2.   
  3.  Tall (80-20) | Weight(5-10) | Tall(190-130)