Hi @ahmed salah ,
Please refer below:
DECLARE @Sql nvarchar(max)
DECLARE @ConStr nvarchar(max)
SET @ConStr= STUFF((SELECT CONCAT(' Or (PM.ZfeatureKey= ', CC.ZfeatureKey ,
IIF(CC.ZfeatureType='Qualifications',' And AcceptedValuesOption_Value ' ,' And ' + case when PATINDEX('%[><=]%', EStrat)>0 then
case when PATINDEX('%[0-9]%', EStrat)>0 then
'AcceptedValuesOption_Value '
else
'cast(AcceptedValuesOption_Value as varchar(100)) ' end
when EStrat is null then
'Name '
else
'Name '
end ) , case when PATINDEX('%[><=]%', EStrat)>0 then
CAST(EStrat AS NVARCHAR(2500))
when EStrat is null then
' is null '
else
''+CAST(EStrat AS NVARCHAR(2500)) +''
end ,')')
FROM #Condition CC INNER JOIN #Allfeatures AL ON AL.ZfeatureKey = CC.ZfeatureKey AND AL.IsNumericValue =0
FOR XML PATH(''), TYPE).value('(./text())[1]','varchar(max)'),1,3,'')
----------------
SET @Sql= CONCAT(' SELECT PartID, Code, Count(1) as ConCount
FROM #PartAttributes PM
INNER JOIN #Condition Co ON Co.ZfeatureKey = PM.ZfeatureKey ',
'Where 1=1 and (', @ConStr, ' ) Group By PartID,Code ' ,
' Having Count(1)> = ',(SELECT COUNT(1) FROM #Condition))
--print @SQL
EXEC (@SQL)
Output:
PartID Code ConCount
4977808 8535400000 4
4977935 8535400000 4
4977936 8535400000 4
4977941 8535400000 4
4979054 8535400000 4
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
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.