Get columns for summary
DECLARE @cols NVARCHAR (MAX) SELECT @cols = COALESCE (@cols + ',[' + ConditionCode + ']', '[' + ConditionCode + ']') FROM (SELECT DISTINCT ConditionCode FROM
Use CROSS JOIN to get ConditionCode and PercentagePrice for each PartCode
DECLARE @query NVARCHAR(MAX) SET @query = 'SELECT PartCode,' + @cols + ' FROM ( SELECT PARTCODE,ConditionCode, CAST(UnitPrice + CAST(PercentagePrice AS DECIMAL(18,2))/100 AS DECIMAL(18,2)) VALUE FROM #tblPart CROSS JOIN #tblPriceCondition ) x PIVOT ( MIN(VALUE) FOR ConditionCode IN (' + @cols + ') ) p ' EXEC SP_EXECUTESQL @query
Please answer what to do with negative values.
Will be updated.
source share