Revision 1d187731-2731-4cb2-923f-a3f0f4ccc272 - Stack Overflow
CREATE FUNCTION samplefunc()
RETURNS TABLE(ntested int, rawscore int, growth int) AS
$func$
SELECT count(distinct student_id) -- AS NTested
,avg(raw_score)::int -- AS RawScore
,avg(growth) -- AS Growth
FROM reports_results
WHERE test_type_id = 1
AND test_id = '201403MAME04'
$func$ LANGUAGE sql;
- The clause to return a table is [**`RETURNS TABLE`**][1]
Also:
- column aliases are only visible inside the function. If you are not going to reference them *inside* the function, they are just documentation.
- What's with the capitalization? Unquoted identifiers are cast to lower case in Postgres automatically.
[1]: http://www.postgresql.org/docs/current/interactive/sql-createfunction.html