2010/1/13 Jaime Casanova <[email protected]>:
> 2010/1/13 Jorge Diaz <[email protected]>:
>>
>> Sin embargo me piden mostrar, el primer y el ultimo día de cada semana, un
>> ejemplo seria asi:
>>
>> Num  Fec_Inicial  Fec_Final
>> 01     03/01/2010  09/01/2010
>> 02     10/01/2010  16/01/2010
>>
>> Desde que inicia el año hasta que termina.
>>
>
> Si estas usando postgres 8.4 esto te serviria:
>

esto me gusto, si necesitas hacer este cada año usa esta función:

create or replace function fechas_anio(in anio int, out num int, out
fecha_inicial date, out fecha_final date)
returns setof record as
$$
    with recursive fechas(n, fi, ff) as (
         select extract(week from f1), f1, f1 + 6
            from (select (($1 || '-02-01')::date -
                         (extract(week from ($1 || '-02-01')::date) -
1 || ' weeks')::interval)::date -
                         (extract(dow from ($1 ||
'-02-01')::date)::integer - 1) as f1
                 ) as foo
         union all
         select n + 1, ff + 1, ff + 7 from fechas
          where extract(year from ff) = $1
 )
 select n::int, fi, ff from fechas;
 $$ language sql


y luego la ejecutas asi:

 select to_char(num, '09'), fecha_inicial, fecha_final from fechas_anio(2011)

-- 
Atentamente,
Jaime Casanova
Soporte y capacitación de PostgreSQL
Asesoría y desarrollo de sistemas
Guayaquil - Ecuador
Cel. +59387171157
--
TIP 1: para suscribirte y desuscribirte, visita 
http://archives.postgresql.org/pgsql-es-ayuda

Responder a