RE: Ayuda con Select

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


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
http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Andrés P.P. 2010-05-07 21:45:07 Re: Ayuda con Select
Previous Message Fernando Hevia 2010-05-07 20:58:03 RE: Ayuda con Select