2009/8/18 Daniel Falcão <[email protected]>:

> Tenho a seguinte tabela com os dados:
>
> create table teste(id serial primary key, data date, quant int);
> insert into teste(data, quant) values('2009-01-01', 12);
> insert into teste(data, quant) values('2009-01-01', 22);
> insert into teste(data, quant) values('2008-01-01', 45);
> insert into teste(data, quant) values('2000-01-01', 15);
> insert into teste(data, quant) values('2002-01-01', 56);
> insert into teste(data, quant) values('2005-01-01', 22);
>
> qual a melhor forma de conseguir o resultado abaixo (de preferencia, sem
> criar uma function)?
>
> ano | soma
> ----------------
> 2009 | 34
> 2008 | 45
> 2007 | 0
> 2006 | 0

<corte>

SELECT EXTRACT(year FROM data), COUNT(id)
   FROM teste
 GROUP BY EXTRACT(year FRIN data);

-Leo
-- 
Leonardo Cezar
http://www.aslid.org.br
http://postgreslogia.wordpress.com
http://www.dextra.com.br/postgres
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a