CREATE PROC [dbo].[sp_GetSurfaceFieldAggregateDIFF]
( 
	@surfaceItemId INT
	,@actionValue VARCHAR(255)
)
AS

--DECLARE @surfaceItemId INT = 37711
--		,@actionValue VARCHAR(255) = '442|443|'

SELECT	ROW_NUMBER() OVER(ORDER BY F.recId) rowId
		,CASE F.surfaceFieldTypeId 
			WHEN 4
			THEN D.surfaceFieldValueNum
			WHEN 5
			THEN D.surfaceFieldValueDecimal
			END val
INTO	#TempValues		
FROM	pal_SurfaceField F
		JOIN pal_SurfaceFieldData D
			ON D.surfaceFieldID = F.recId
WHERE	D.surfaceItemId = @surfaceItemId
		AND F.recId IN (SELECT splitdata FROM SplitString(@actionValue,'|'))
ORDER BY F.sequence
		
SELECT top 1 (t2.val - t1.val) AS sub1
FROM #TempValues t1 CROSS JOIN
     #TempValues t2
WHERE t1.rowId <> t2.rowId

DROP TABLE #TempValues
GO


