2009/8/18 Leonardo Cezar <[email protected]>:
> On Tue, Aug 18, 2009 at 2:53 PM, Leonardo Cezar<[email protected]> wrote:
>> <corte>
>>
>> SELECT EXTRACT(year FROM data), COUNT(id)
>>   FROM teste
>>  GROUP BY EXTRACT(year FRIN data);
>
> Complementando:
>
> http://www.postgresql.org/docs/8.4/static/tutorial-sql.html
>


Complementando o que já foi dito:
Para você exibir os anos inexistentes em sua tabela faça um JOIN com a
função generate_series.
http://www.postgresql.org/docs/current/interactive/functions-srf.html

Com seus dados:
bdteste=# SELECT ano, sum(coalesce(quant,0)) FROM (SELECT
generate_series(menor,maior) FROM (SELECT min(extract(year from
data))::int AS menor, max(extract(year from data))::int AS maior FROM
teste) foo) bar(ano) LEFT OUTER JOIN teste ON ano = extract(year from
teste.data)::int GROUP BY ano ORDER BY ano;
 ano  | sum
------+-----
 2000 |  15
 2001 |   0
 2002 |  56
 2003 |   0
 2004 |   0
 2005 |  22
 2006 |   0
 2007 |   0
 2008 |  45
 2009 |  34
(10 registros)

Osvaldo
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a