function sum (text) does not exist

0

I have a very simple function that groups together some values of the Chile 2017 census. The columns of interest say a2007, a2008, a2009, etc that are the years and their population, all are of the numeric type but not the problem that does not I can call the column, the error appears

  

ERROR: function sum (text) does not exist   LINE 1: select sum (date_aux) from demo where age be ...                  ^   HINT: No function matches the given name and argument types. You might need to add explicit type casts.   QUERY: select sum (date_aux) from demo where age between $ 1 and $ 2   CONTEXT: PL / pgSQL function demo_grupada (numeric, numeric) line 18 at SQL statement   SQL state: 42883

The function is:

    CREATE OR REPLACE FUNCTION demo_agrupada(edad_inicial numeric, edad_final numeric) 
RETURNS TABLE (agno text, poblacion numeric) AS $$
DECLARE
r text;
v numeric;
fecha_aux text;
fecha numeric;

BEGIN
for r in select ct.fecha,ct.texto from (values (2007,'a2007'),(2008,'a2008'),(2009,'a2009'),(2010,'a20010'),(2011,'cinco'),(2012,'cinco')
              ,(2013,'cinco'),(2014,'cinco'),(2015,'cinco'),(2016,'cinco'),(2017,'cinco')) as ct(fecha,texto)
LOOP
agno:=r;

fecha_aux='a'||r;
fecha_aux='"'||fecha_aux||'"';
--fecha_aux=fecha_aux::numeric;
--raise notice 'Value: %',r;
select sum(fecha_aux) into poblacion from demo where edad between $1 and $2;
return;
END LOOP;
END;
$$ LANGUAGE plpgsql;

can someone help me?

    
asked by Gonzalo 12.09.2018 в 19:24
source

0 answers