我有一个人员列表,我希望获得该年度财产日期按年份分组的人数。 问题是我想要寻找的年数是从当前的向后我希望用户选择它。 目前我获取了10年前的信息,但我希望用户可以输入任意年数。
CREATE OR REPLACE FUNCTION convertidos_neto_10_year_ago(IN aidiglesia integer)
RETURNS TABLE(cant0 bigint, cant1 bigint, cant2 bigint, cant3 bigint, cant4 bigint, cant5 bigint, cant6 bigint, cant7 bigint, cant8 bigint, cant9 bigint, cant10 bigint) AS
$BODY$BEGIN
RETURN Query SELECT
( SELECT count(jb_persona.id)
FROM jb_persona
WHERE ((jb_persona.id_iglesiafk = aidiglesia) AND (date_part('year'::text, jb_persona.fecha_conversion) = date_part('year'::text, ('now'::text)::date) - (10)::double precision))) ,
( SELECT count(jb_persona.id)
FROM jb_persona
WHERE ((jb_persona.id_iglesiafk = aidiglesia) AND (date_part('year'::text, jb_persona.fecha_conversion) = (date_part('year'::text, ('now'::text)::date) - (9)::double precision)))) ,
( SELECT count(jb_persona.id) AS count3
FROM jb_persona
WHERE ((jb_persona.id_iglesiafk = aidiglesia) AND (date_part('year'::text, jb_persona.fecha_conversion) = (date_part('year'::text, ('now'::text)::date) - (8)::double precision)))) ,
( SELECT count(jb_persona.id) AS count4
FROM jb_persona
WHERE ((jb_persona.id_iglesiafk = aidiglesia) AND (date_part('year'::text, jb_persona.fecha_conversion) = (date_part('year'::text, ('now'::text)::date) - (7)::double precision)))) ,
( SELECT count(jb_persona.id) AS count5
FROM jb_persona
WHERE ((jb_persona.id_iglesiafk = aidiglesia) AND (date_part('year'::text, jb_persona.fecha_conversion) = (date_part('year'::text, ('now'::text)::date) - (6)::double precision)))) ,
( SELECT count(jb_persona.id) AS count5
FROM jb_persona
WHERE ((jb_persona.id_iglesiafk = aidiglesia) AND (date_part('year'::text, jb_persona.fecha_conversion) = (date_part('year'::text, ('now'::text)::date) - (5)::double precision)))) ,
( SELECT count(jb_persona.id) AS count5
FROM jb_persona
WHERE ((jb_persona.id_iglesiafk = aidiglesia) AND (date_part('year'::text, jb_persona.fecha_conversion) = (date_part('year'::text, ('now'::text)::date) - (4)::double precision)))) ,
( SELECT count(jb_persona.id) AS count5
FROM jb_persona
WHERE ((jb_persona.id_iglesiafk = aidiglesia) AND (date_part('year'::text, jb_persona.fecha_conversion) = (date_part('year'::text, ('now'::text)::date) - (3)::double precision)))) ,
( SELECT count(jb_persona.id) AS count5
FROM jb_persona
WHERE ((jb_persona.id_iglesiafk = aidiglesia) AND (date_part('year'::text, jb_persona.fecha_conversion) = (date_part('year'::text, ('now'::text)::date) - (2)::double precision)))) ,
( SELECT count(jb_persona.id) AS count5
FROM jb_persona
WHERE ((jb_persona.id_iglesiafk = aidiglesia) AND (date_part('year'::text, jb_persona.fecha_conversion) = (date_part('year'::text, ('now'::text)::date) - (1)::double precision)))) ,
( SELECT count(jb_persona.id) AS count5
FROM jb_persona
WHERE ((jb_persona.id_iglesiafk = aidiglesia) AND (date_part('year'::text, jb_persona.fecha_conversion) = (date_part('year'::text, ('now'::text)::date) - (0)::double precision))));
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION convertidos_neto_10_year_ago(integer)
OWNER TO postgres;
答案 0 :(得分:1)
在输出外部连接处也有零计数年份生成的年份系列:
select jsonb_object_agg(year, total)
from (
select to_char(d, 'YYYY') as year, count(id) as total
from
jb_persona
right join
generate_series(
date_trunc('year', now() - interval '10 years'),
now(), interval '1 year'
) gs(d) on gs.d = date_trunc('year', fecha_conversion)
where id_iglesiafk = aidiglesia
group by 1
) s
如果您希望将列作为最简单的解决方案,请将其作为json:
select jsonb_object(array_agg(year), array_agg(total::text))
from ...
用于9.4:
CORS