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: fhe...@ip-tel.com.ar
> To: fs...@hotmail.com
> CC: pgsql-es-ayuda@postgresql.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

Responder a