This is a solution without creating implementation of any not build-in functions. I know your example will be more complex but this is just an idea:
CREATE TABLE DataSource
(
[ID] TINYINT
,[Value] INT
)
INSERT INTO DataSource ([ID], [Value])
VALUES (1, 2)
,(1, 0)
,(1, NULL)
,(1, 98)
,(1, NULL)
,(2, -4)
,(2, 0)
,(2, 0)
,(2, NULL)
SELECT [ID]
,MAX([Value])
FROM
(
SELECT [ID]
,AVG(COALESCE([Value],0))
FROM DataSource
GROUP BY [ID]
UNION ALL
SELECT DISTINCT [ID]
,0
FROM DataSource
) Data([ID],[Value])
GROUP BY [ID]
Here is the fiddle - http://sqlfiddle.com/#!6/3d223/14