2009/7/30 fabio.ebner <[email protected]>:
> Pessoal.. preciso do seguinte:
>
> eu tenho uma tabela de selos, que vai de 0 ate x.. alguns selos podem estar 
> danificados.. entao eu faco o seguinte select "SELECT * FROM tb_selo WHERE 
> ic_selo_danificado  = fase;"
>
> perfeito.. porem nesse caso ele me traz um selo por linha.. exemplo:
> 0
> 1
> 2
> 3
>  ** o 4 esta danificado logo nao retorna
> 5
> 6
> 7
> 8
> 9
> 10
> 11
> 12
>
>
> porem eu queria q no meu select retornasse algo assim
>
> 0..3
> 5..12
>
> entenderam?? que ele agrupasse do primeiro ate aonde tiver o prox..
>
> existe alguma coisa facil para fazer isso ou tenho q fazer um function??
>


Modifiquei um pouco os dados para testar as condições de contorno. Tente:

bdteste=# CREATE TEMP TABLE tb_selo(
bdteste(#   num  int  PRIMARY KEY,
bdteste(#   danificado boolean);
NOTA:  CREATE TABLE / PRIMARY KEY criará índice implícito
"tb_selo_pkey" na tabela "tb_selo"
CREATE TABLE
bdteste=#
bdteste=# INSERT INTO tb_selo VALUES
(0,FALSE),(1,TRUE),(2,FALSE),(3,FALSE),(4,TRUE),(5,FALSE),(6,FALSE),(7,FALSE),(8,FALSE),(9,FALSE),(10,FALSE),(11,FALSE),(12,FALSE),(13,TRUE),(14,FALSE);
INSERT 0 15
bdteste=#
bdteste=# SELECT * FROM tb_selo;
 num | danificado
-----+------------
   0 | f
   1 | t
   2 | f
   3 | f
   4 | t
   5 | f
   6 | f
   7 | f
   8 | f
   9 | f
  10 | f
  11 | f
  12 | f
  13 | t
  14 | f
(15 registros)

bdteste=# SELECT t1.num AS incio, t2.num AS fim
bdteste-#   FROM tb_selo t1 INNER JOIN tb_selo t2
bdteste-#                   ON (t1.num <= t2.num)
bdteste-#  WHERE NOT EXISTS (SELECT * FROM tb_selo t3
bdteste(#                     WHERE (t3.num BETWEEN t1.num AND t2.num
AND t3.danificado = TRUE)
bdteste(#                        OR (t3.num = t2.num + 1 AND
t3.danificado = FALSE)
bdteste(#                        OR (t3.num = t1.num - 1 AND
t3.danificado = FALSE));
 incio | fim
-------+-----
     0 |   0
     2 |   3
     5 |  12
    14 |  14
(4 registros)

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

Responder a