Assuming your table has the following structure:
CREATE TABLE tb_ano_mes
(
id bigserial,
ano int,
mes int,
CONSTRAINT pk_ano_mes PRIMARY KEY ( id )
);
Containing the following data:
INSERT INTO tb_ano_mes ( id, mes, ano ) VALUES ( 1, 1, 2005 );
INSERT INTO tb_ano_mes ( id, mes, ano ) VALUES ( 2, 2, 2005 );
INSERT INTO tb_ano_mes ( id, mes, ano ) VALUES ( 3, 4, 2005 );
INSERT INTO tb_ano_mes ( id, mes, ano ) VALUES ( 4, 5, 2006 );
Your problem can be solved with the function generate_series()
, supported since version 9.1
postgres:
SELECT
EXTRACT( MONTH FROM s)::int AS mes,
EXTRACT( YEAR FROM s )::int AS ano
FROM
generate_series( '01/01/2005', '31/12/2015', '1 month'::interval ) AS s
LEFT JOIN
tb_ano_mes AS tbl ON ( EXTRACT(MONTH FROM s) = tbl.mes AND EXTRACT(YEAR FROM s) = tbl.ano )
WHERE
tbl.id IS NULL
ORDER BY
ano,
mes;
I hope I’ve helped!
This is not a rule, the date format depends on how the 'Datestyle' environment variable is set in the client. Reference: https://www.postgresql.org/docs/9.5/static/runtime-config-client.html#GUC-DATESTYLE
– Lacobus