You can keep that as a SQL function if you want to. SQL functions can return multiple rows by running multiple SELECT queries, but they can only return multiple columns from the same query. So in order to keep that a SQL function, you need to use a single query that returns all the columns. This could be achieved with a common table expression:
CREATE FUNCTION public.getpogstats(IN p_symbol character varying, IN p_pogtypeid integer, OUT closehi numeric, OUT closelo numeric, OUT dayhi numeric, OUT daylo numeric, OUT s7dhi numeric, OUT s7dlo numeric, OUT t13hi numeric, OUT t13lo numeric, OUT close numeric, OUT firstdate timestamp without time zone)
RETURNS record
LANGUAGE sql
AS $function$
with data1 as (
SELECT ROUND(MAX(closeprice), 2) as closehi,
ROUND(MIN(closeprice), 2) as closelo,
ROUND(MAX(dayhigh), 2) as dayhi,
ROUND(MIN(daylow), 2) as daylo,
ROUND(MAX(sevendaydp), 2) as s7dhi,
ROUND(MIN(sevendaydp), 2) as s7dlo,
ROUND(MAX(thirteendaydp), 2) as t13hi,
ROUND(MIN(thirteendaydp), 2) as t13lo,
MIN(datadate) as firstdate
FROM pogdata
JOIN symbol ON pogdata.symbolid = symbol.symbolid AND pogdata.pogtypeid = p_pogtypeid
WHERE symbol.symbol = p_symbol
), data2 as (
SELECT ROUND(closeprice, 2) as close
FROM pogdata
JOIN symbol ON pogdata.symbolid = symbol.symbolid
WHERE datadate = (SELECT MAX(datadate)
FROM pogdata
JOIN symbol ON pogdata.symbolid = symbol.symbolid AND pogdata.pogtypeid = p_pogtypeid
WHERE symbol.symbol = p_symbol)
AND symbol.symbol = p_symbol
LIMIT 1 -- just to be sure
)
select d1.closehi, d1.closelo, d1.dayhi, d1.daylo, d1.s7dhi, d1.s7dlo, d1.t13hi, d1.t13lo, d2.close, d1.firstdate
from data1 as d1
cross join data2 as d2;
$function$;
(I have the impression the two queries can be combined into one, but right now I can't think of a way to do that)
Note that if you want to have a result with multiple columns (rather then one column with multiple fields), you need to expand those explicitly.
The following select:
select getpogstats('foo', 1);
will return a single row with a single column containing multiple fields, something like:
getpogstats
-----------
(1,2,3,4,5,6,7,8,9,"2017-05-09 18:19:20")
because the function is declared as "RETURNS record".
However, if you want the result as individual columns, you need to use:
select (getpogstats('foo', 1)).*;
Then you'll get each column separately:
closehi | closelo | dayhi | ...
--------+---------+-------+----
1 | 2 | 3 | ...
Typically, functions returning more then one column are easier to handle if you declare them as returns table (...):
CREATE FUNCTION public.getpogstats(p_symbol character varying, p_pogtypeid integer)
returns table(closehi numeric, closelo numeric, dayhi numeric, daylo numeric, s7dhi numeric, s7dlo numeric, t13hi numeric, t13lo numeric, close numeric, firstdate timestamp without time zone)
LANGUAGE sql
AS $function$
with data1 as (
SELECT ROUND(MAX(closeprice), 2) as closehi,
ROUND(MIN(closeprice), 2) as closelo,
ROUND(MAX(dayhigh), 2) as dayhi,
ROUND(MIN(daylow), 2) as daylo,
ROUND(MAX(sevendaydp), 2) as s7dhi,
ROUND(MIN(sevendaydp), 2) as s7dlo,
ROUND(MAX(thirteendaydp), 2) as t13hi,
ROUND(MIN(thirteendaydp), 2) as t13lo,
MIN(datadate) as firstdate
FROM pogdata
JOIN symbol ON pogdata.symbolid = symbol.symbolid AND pogdata.pogtypeid = p_pogtypeid
WHERE symbol.symbol = p_symbol
), data2 as (
SELECT ROUND(closeprice, 2) as close
FROM pogdata
JOIN symbol ON pogdata.symbolid = symbol.symbolid
WHERE datadate = (SELECT MAX(datadate)
FROM pogdata
JOIN symbol ON pogdata.symbolid = symbol.symbolid AND pogdata.pogtypeid = p_pogtypeid
WHERE symbol.symbol = p_symbol)
AND symbol.symbol = p_symbol
LIMIT 1 -- just to be sure
)
select d1.closehi, d1.closelo, d1.dayhi, d1.daylo, d1.s7dhi, d1.s7dlo, d1.t13hi, d1.t13lo, d2.close, d1.firstdate
from data1 as d1
cross join data2 as d2;
$function$;
Then you can use:
select *
from getpogstats('foo', 1);
and the result will automatically have a "table like" structure.
select ... intocan only be used with PL/pgSQL, not SQL (also - unrelated - the language name is an identifier it should not be put between single quotes, so it should belanguage sqlorlanguage plpgsql)