2010/4/23 Thiago <zan...@farmaponte.com.br>

> Galera, bom dia.
>
> Tenho o seguinte select:
>
> select *
> from dp.tb_apont_func_historico h
> where h.int_ano >= 2010 and h.int_mes >= 2
>
> Ele está me retornando o seguinte resultado:
>
> ano     mes     codfun
> 2010    4       1593
> 2010    4       1836
> 2010    2       1836
> 2010    4       2398
>
> Acontece que o codigo 1836 aparece 2x na pesquisa, eu gostaria que
> aparecesse apenas 1x cada codfun, mas que sempre trouxesse o menor ano,mes.
>
> Estou quebrando a cabeça aqui mas estou totalmente perdito neste group.
>
> Alguém saberia como me ajudar?
>

create table t1(ano char(4),mes char(2),codfun int);
copy t1 from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>>2010 4       1593
>>2010 4       1836
>>2010 2       1836
>>2010 4       2398
>>\.

select * from t1;

 ano  | mes | codfun
------+-----+--------
 2010 | 4   |   1593
 2010 | 4   |   1836
 2010 | 2   |   1836
 2010 | 4   |   2398
(4 rows)

select ano,min(mes),codfun from t1 where ano>='2010' group by ano,codfun;

 ano  | min | codfun
------+-----+--------
 2010 | 2   |   1836
 2010 | 4   |   1593
 2010 | 4   |   2398
(3 rows)


-- 
Marcelo Costa
www.marcelocosta.net
-------------------------------------------------
“You can't always get what you want”,

Doctor House in apology to Mike Jagger
_______________________________________________
pgbr-geral mailing list
pgbr-geral@listas.postgresql.org.br
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a