I have some complex function that I want to use in number of queries. It gets some list of values and return aggregate value.
For example (I simplify it, it is more complex in deed):
CREATE FUNCTION Mean(@N Numbers READONLY)
RETURNS TABLE AS RETURN (
SELECT mean = SUM(n.value) / COUNT(*) FROM @N n
)
and I want to use it in query:
SELECT d.DepartmentName, MeanRate = m.mean
FROM Departments d
CROSS APPLY Mean(
(
SELECT value = e.Rate
FROM Employees e
WHERE e.DepatmentId = d.DepatmentId
)
) m
But I get an error: Operand type clash: float is incompatible with Numbers
I know that I can use cursor or pass values as XML, but I think this ways are slower than inline function and table variables.
How can I pass a list of values to inline function?