You can use a view or Common Table Expression to get the top 4 and then SUM .
SELECT SUM(Value) As Top4Sum FROM ( SELECT TOP (4) Value FROM YourTable WHERE Code = 'A' ORDER BY Value DESC ) T
If you want a SUM for TOP 4 for each Code , you could do
;WITH CTE AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY Code ORDER BY Value DESC) RN FROM YourTable) SELECT Code, SUM(Value) FROM CTE WHERE RN <= 4 GROUP BY Code
source share