Code:
SELECT UserDefinedProperties.nID, UserDefinedProperties.sValue, UserDefinedProperties.SContentID, PreCutProds.[Short description], PreCutProds.ManPrecut, PreCutProds.PrecutCat, PreCutProds.PreCutCatFinal, PreCutProds.TypePrecutnIDFROM UserDefinedProperties INNER JOIN (SELECT First(Product.[Short description]) AS [Short description], Product.[Product Reference], First(UDP.SContentID) AS FirstOfSContentID, First(UDP.sValue) AS FirstOfsValue, First(UDP.sName) AS FirstOfsName, First(UDP.nID) AS FirstOfnID, Max(IIf(UDP.sName='Type of Precut',UDP.sValue,Null)) AS TypePrecut, Max(IIf(UDP.sName='Type of Precut',UDP.nID,Null)) AS TypePrecutnID, Max(IIf(UDP.sName='Category',UDP.sValue,Null)) AS Precut, Max(IIf(InStr(Product.[Short description],'Jelly Roll')>0,'JELLY ROLL',IIf(InStr(Product.[Short description],'Charm Pack')>0,'CHARM PACK',IIf(InStr(Product.[Short description],'Layer Cake')>0,'LAYER CAKE',Null)))) AS PrecutCat, Max(IIf(UDP.sName='brand',UDP.svalue,Null)) AS ManPrecut,
IIf(IsNull(PrecutCat),
iif(instr(ManPrecut, 'Hoffman')>0, 'HOFFMAN',
iif(instr(ManPrecut, 'Lewis & Irene')>0, 'LEWIS & IRENE', 'OTHER')),PrecutCat) AS PreCutCatFinal
FROM Product INNER JOIN (SELECT UserDefinedProperties.nID, UserDefinedProperties.SContentID, UserDefinedProperties.sValue, Variable.sName, UserDefinedProperties.nID
FROM UserDefinedProperties LEFT JOIN Variable ON UserDefinedProperties.nVariableID = Variable.nID
WHERE ((Variable.sName='Category' and UserDefinedProperties.sValue = 'PRE-CUT') or Variable.sName='Type of Precut' or Variable.sName='brand') AND UserDefinedProperties.nContentLevel = 2 ) AS UDP ON Product.[Product Reference] = UDP.SContentID
GROUP BY Product.[Product Reference]
HAVING (((Product.[Product Reference]) Not Like '*!*') AND ((Max(IIf([UDP].[sName]='Category',[UDP].[sValue],Null))) Is Not Null))
ORDER BY First(Product.[Short description])
) AS PreCutProds ON UserDefinedProperties.nID = PreCutProds.TypePrecutnID
WHERE UserDefinedProperties.sValue <> PreCutProds.PreCutCatFinal and UserDefinedProperties.nID = PreCutProds.TypePrecutnID;
I have saved the query as "QUERY3" just to make it easier to read in this question. I am using this as part of an update query: