RE: Ayuda con Select

From: Fernando Siguenza <fsigu(at)hotmail(dot)com>
To: <solopostgres(at)gmail(dot)com>
Cc: Foro Postgres <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: RE: Ayuda con Select
Date: 2010-05-07 21:53:28
Message-ID: SNT108-W504D00CF2D3B6E25640A41A1F60@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda


Gracias Andres, funciono bien, ahora una ultima consulta es que me quede picado jeje.

1) Hay com oobtener el mismo resultado usando crosstab??

2) Si quisiera hacer dinamico la funcion es decir que me den hasta cuantos dias se desea revisar la disponibilidad, como podria hacerlo?

Saludos

Date: Fri, 7 May 2010 17:45:07 -0400
Subject: Re: [pgsql-es-ayuda] Ayuda con Select
From: solopostgres(at)gmail(dot)com
To: fsigu(at)hotmail(dot)com
CC: pgsql-es-ayuda(at)postgresql(dot)org


Fernando..

..talvez no entendí bien , de lo que vi creo que esto te serviría...

select larcod as Largo,
count(*) as Total,
sum(CASE when extract(day from now() - fecha) = 0 then 1 else 0 end) as Dia0,
sum(CASE when extract(day from now() - fecha) = 1 then 1 else 0 end) as Dia1,
sum(CASE when extract(day from now() - fecha) = 2 then 1 else 0 end) as Dia2,
sum(CASE when extract(day from now() - fecha) = 3 then 1 else 0 end) as Dia3,
sum(CASE when extract(day from now() - fecha) = 4 then 1 else 0 end) as Dia4,
sum(CASE when extract(day from now() - fecha) = 5 then 1 else 0 end) as Dia5
from tabla_flores
group by larcod order by 1;

prueba y avisa..

Saludos
Andrés.

El 7 de mayo de 2010 17:13, Fernando Siguenza <fsigu(at)hotmail(dot)com> escribió:

Gracias amigo por la respuesta, estoy justo analizando la funcion, pero no logro dar con el resultado deseado, tengp esta consulta y no obtengo lo que quiero

select *
from crosstab(
'select bunlarcod,count(buncod),current_date-bunfec as dia
from bunche
where bunfec>=current_date-3 and bunfec<=current_date
group by bunfec,bunlarcod')
AS bunche(bunlarcod int,dia int,dia1 int)

que me da esto

bunlarcod
dia
dia1

20
2

10
3

20
3

y yo quiero algo como esto.

largo
Total
dia0
dia1
dia2
dia3
dia4
dia5

10
1
0
0
0
1
0
0

20
5
0
0
1
4
0
0



Ojala y me puedan ayudar a armar el select.

Saludos.

he probado tambien con un select normay y algunos case, con esta consulta

select bunlarcod,count(buncod)as dispo,
case when bunfec=current_date-1 then count(buncod) else 0 end as dia0,
case when bunfec=current_date-2 then count(buncod) else 0 end as dia1,
case when bunfec=current_date-3 then count(buncod) else 0 end as dia2,
case when bunfec=current_date-4 then count(buncod) else 0 end as dia3,
case when bunfec=current_date-5 then count(buncod) else 0 end as dia4,
case when bunfec=current_date-6 then count(buncod) else 0 end as dia5,
case when bunfec=current_date-7 then count(buncod) else 0 end as dia6
from bunche
group by bunfec,bunlarcod

pero obtengo esto

bunlarcod
dispo
dia0
dia1
dia2
dia3
dia4
dia5

10
1
0
0
0
1
0
0

20
5
0
0
1
0
0
0

20
5
0
0
0
4
0
0


qaue tampoco es lo que quiero se me repite el largo cuando deberia salir solo un registro del largo 20.

Saludos


> From: fhevia(at)ip-tel(dot)com(dot)ar
> To: fsigu(at)hotmail(dot)com
> CC: pgsql-es-ayuda(at)postgresql(dot)org
> Subject: RE: [pgsql-es-ayuda] Ayuda con Select
> Date: Fri, 7 May 2010 17:58:03 -0300

>
>
>
> > -----Mensaje original-----
> > De: Fernando Siguenza
> >
> > Amigos como estan, en otro foro me ayudaron con la respuesta
> > de lo que necesito pero solo que esta en sqlserver, ahi veo
> > que hay una funcion pivot que hace lo que necesito, la
> > consulta es algo como la siguiente select bunlarcod,Total =
> > [0]+[1]+[2]+[3]+[4]+[5],dia0 = [0], dia1 = [1], dia2 = [2],
> > dia3 = [3], dia4 = [4], dia5 = [5] from ( select bunlarcod,
> > buncod, datediff(dd, bunfec, getdate()) as dias from bunche
> > where bunfec >= dateadd(dd, -5,
> > cast(convert(char(8),getdate(), 112) as smalldatetime))
> > ) T
> > PIVOT (count(buncod) FOR dias in ([0], [1], [2], [3], [4], [5])) as P
> >
> > Ahora mi duda Hay alguna funcion parecida a la pivot en postgre???
> >
> > Saludos
> >
>
> Hola. En Postgres es el modulo tablefunc y sus funciones crosstab:
>
> http://www.postgresql.org/docs/8.4/interactive/tablefunc.html
>
> Slds.
>
>

Explore the seven wonders of the world Learn more!

_________________________________________________________________
Discover the new Windows Vista
http://search.msn.com/results.aspx?q=windows+vista&mkt=en-US&form=QBRE

In response to

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Jaime Casanova 2010-05-08 05:41:43 Re: Problema con ODBC
Previous Message Andrés P.P. 2010-05-07 21:45:07 Re: Ayuda con Select