Hi,@ahmed salah
Below is the demo data you provide:
insert into FinalTableData
values (35819351, 1, 'RoHS (2015/863)', 'Compliant with Exemption', 340434330, 'Contact', 1362938, 1),
(35819351, 2, 'RoHS (2015/863)', 'Compliant', 288530768, 'Web Page', 1232162, 1),
(35819351, 3, 'RoHS (2015/863)', 'NotCompliant', 288539070, 'Coc', 1232160, 1),
The Row_Number of 'Web Page' is 2 in the demo data ,which does not meet your priority requirements.
So you may need to re-rank Row_number ,as I answered in your last thread:
;WITH CTE AS
(
SELECT PartId,Regulation,Comp_Status,REVID ,Doc_Type,Document_Type,ComplianceTypeID,
ROW_NUMBER()OVER(PARTITION BY ComplianceTypeID ORDER BY CHARINDEX(Doc_Type+ ', ','Web Page,Coc,Contact, ')) AS Row_Number
FROM #FinalTableData
)SELECT ...
FROM #TempTable MD
LEFT OUTER JOIN
CTE Rohs ON MD.PartID = Rohs.PartID
AND Rohs.ComplianceTypeID = 1
AND Rohs.Row_Number = 1
LEFT OUTER JOIN
CTE Reach ON MD.PartID = Reach.PartID
AND Reach.ComplianceTypeID = 2
AND Reach.Row_Number = 1
LEFT OUTER JOIN
CTE TSKA ON MD.PartID = TSKA.PartID
AND TSKA.ComplianceTypeID = 11
AND TSKA.Row_Number = 1
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.