ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 251.8k

How to make join to teams display without join all teams ?

Mar 28 2020 3:37 PM
How to make left join to only teams displayed dynamically depend on fields exist on @selectedcolumncomma ?
i work on SQL server 2012 my problem how to generate join dynamically based on teams displayed without writing all
 
relations to all teams and my be result two teams only
 
on my situation below i make left join to 7 teams because may be one team from 7 come
 
so actually i need left join dynamically based on column generated dynamically
 
so if i one team displayed to lifecycle then one left join to lifecycle
 
so if i two team displayed to lifecycle and crosspart as my sample then two left join to lifecycle and crosspart
  1. create table #tempVariables  
  2. (  
  3. DocumentId int  
  4. )  
  5. insert into #tempVariables(DocumentId) values (22490)  
  6. create table #Teams  
  7. (  
  8. TeamId int,  
  9. TeamName nvarchar(50),  
  10. ColumnName  nvarchar(100)  
  11. )  
  12. insert into #teams   
  13. values  
  14. (1,'Package','Package'),  
  15. (2,'Parametric','Parametric'),  
  16. (3,'Scribing','Scribing'),  
  17. (4,'Lifecycle','Lifecycle'),  
  18. (5,'OBS','OBS'),  
  19. (6,'Cross','CrossPart'),  
  20. (7,'Rohs','Rohs')  
  21.   
  22. create table #DocumentTeams  
  23. (  
  24. DocumentTeamId int identity(1,1),  
  25. DocumentId int,  
  26. TeamId nvarchar(50)  
  27. )  
  28. insert into #DocumentTeams(DocumentId,TeamId)   
  29. values  
  30. (22490,4),  
  31. (22490,6),  
  32. (22491,1),  
  33. (22491,5),  
  34. (22491,7)  
  35.   
  36. Create table #FlowStatus  
  37. (  
  38. FlowStatusID int,  
  39. FlowStatus  nvarchar(100)  
  40. )  
  41. insert into #FlowStatus   
  42. values  
  43. (1, 'Pending'),  
  44. (2, 'InProgress'),  
  45. (3, 'Done')  
  46. create table #DocumentPartTeams  
  47. (  
  48. DocumentPartTeamsId int,  
  49. PartId  int,  
  50. DocumentId int,  
  51. Package  int,  
  52. Parametric int,  
  53. Scribing int,  
  54. Lifecycle int,  
  55. OBS int,  
  56. CrossPart int,  
  57. Rohs int  
  58.   
  59. )  
  60. insert into #DocumentPartTeams  
  61.   
  62. (PartId,DocumentId,Package,Parametric,Scribing,Lifecycle,OBS,CrossPart,Rohs)  
  63. values  
  64. (1000,22490,null,null,null,1,null,1,null),  
  65. (1002,22490,null,null,null,1,null,1,null),  
  66. (1005,22491,2,null,null,null,2,null,2),  
  67. (1008,22491,2,null,null,null,2,null,1)  
  68.   
  69. select dt.DocumentID,dt.TeamID, t.TeamName,t.ColumnName into #GetDocumentTeams from     
  70. #DocumentTeams dt   
  71. inner join #Teams t  on t.TeamID=dt.TeamID  
  72. inner join #tempVariables tv on tv.DocumentId=dt.DocumentId 
  73. SELECT distinct ColumnName INTO #COLUMNS FROM #GetDocumentTeams  
  74. select t.TeamId,c.ColumnName into #indexedColumns from #COLUMNS c inner join pcn.Teams t   
  75. on t.ColumnName=c.ColumnName
  76. declare @SeletColumnComma varchar(max)  
  77. select @SeletColumnComma = coalesce(@SeletColumnComma + ',','') + coalesce('fs' +cast   
  78. (teamid as nvarchar(20)) + '.FlowStatus as ' + ColumnName + 'Status',''from   
  79. #indexedColumns  
  80. select @SeletColumnComma  
  81. ---------------
  82. DECLARE @query nvarchar(max)  
  83. SET @query='  
  84. select distinct dpt.PartId,' + @SeletColumnComma + ' from #DocumentPartTeams dpt   
  85. inner join #GetDocumentTeams gdt on gdt.DocumentID=dpt.DocumentID  
  86. inner join #tempVariables tv on tv.DocumentId = dpt.DocumentId  
  87. left join #FlowStatus fs1  on dpt.Package=fs1.FlowStatusID  
  88. left join #FlowStatus fs2  on dpt.Parametric=fs2.FlowStatusID  
  89. left join #FlowStatus fs3  on dpt.Scribing=fs3.FlowStatusID  
  90. left join #FlowStatus fs4  on dpt.Lifecycle=fs4.FlowStatusID  
  91. left join #FlowStatus fs5  on dpt.OBS=fs5.FlowStatusID  
  92. left join #FlowStatus fs6  on dpt.CrossPart=fs6.FlowStatusID  
  93. left join #FlowStatus fs7  on dpt.Rohs=fs7.FlowStatusID  
  94.  '  
  95.   
  96. exec (@query)  
  97.   
  98. drop table #teams  
  99. drop table #DocumentTeams  
  100. drop table #FlowStatus  
  101. drop table #DocumentPartTeams  
  102. drop table #COLUMNS  
  103. drop table #indexedColumns  
  104. drop table #tempVariables  
  105. drop table #GetDocumentTeams  
  106.   
  107.   
  108.   
  109.   
  110.   
  111. Result displayed  
  112.   
  113. PartId  LifecycleStatus CrossPartStatus  
  114. 1000    Pending           Pending  
  115. 1002    Pending           Pending  
  116. so left join or inner join i needed to be only teams displayed dynamically   
  117.   
  118. so how to make that please   

Answers (1)