I created the following function:
CREATE FUNCTION categorize (
@id VARCHAR(5),
@var1_low INT,
@var1_high INT,
@var2_low DECIMAL(18,6),
@var2_high DECIMAL(18,6)
)
RETURNS TABLE
AS RETURN
(SELECT
year,
id,
COUNT(id) AS totalnum,
SUM(score) AS var1_sum
FROM
database1
WHERE
studentid = @id
AND Score >= @var1_low
AND Score <= @var1_high
AND time >= @var2_low
AND time <= @var2_high
GROUP BY
year
ORDER BY
year
)
Then it says
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
How can I solve this issue?