Ver Mensaje Individual
  #19 (permalink)  
Antiguo 12/08/2008, 09:17
Avatar de rj69
rj69
 
Fecha de Ingreso: agosto-2008
Ubicación: Melipilla
Mensajes: 49
Antigüedad: 15 años, 9 meses
Puntos: 0
Exclamación Respuesta: columnas dinamicas en select

estimado, veamos si esto te sirve

este es el query que realizamos nosotros para unir dos tablas, sacando los datos de la segunda para dejarlos como columnas de la primera

select distinct t1.numero as "Folio",t1.mopnrobasc+399 as "instalacion", t1.patente, t1.fecha, t1.hora,t1.tipo,
sum(case(t2.nroeje) when (1) then (t2.peseje) else (0) end) as eje1,
sum(case(t2.nroeje) when (2) then (t2.peseje) else (0) end) as eje2,
sum(case(t2.nroeje) when (3) then (t2.peseje) else (0) end) as eje3,
sum(case(t2.nroeje) when (4) then (t2.peseje) else (0) end) as eje4,
sum(case(t2.nroeje) when (5) then (t2.peseje) else (0) end) as eje5,
sum(case(t2.nroeje) when (6) then (t2.peseje) else (0) end) as eje6,
sum(case(t2.nroeje) when (7) then (t2.peseje) else (0) end) as eje7,
t1.bruto
from mop_cam as t1 left outer join mop_eje as t2
on t1.numero = t2.numero
and t1.mopnrobasc = t2.mopnrobasc
group by t1.numero,t1.mopnrobasc, t1.patente, t1.fecha, t1.hora,t1.tipo,t1.bruto
order by t1.numero

donde la tabla mop_cam tiene los siguientes datos
numero--mopnrobasc--patente--fecha--hora--tipo--bruto
1 1 xx-9999 xxxx xxx 1 20000
2 1 xx-8888 xxxx xxx 1 15000

y la tabla mop_eje tiene
numero--mopnrobasc--nroeje--peseje
1 1 1 5000
1 1 2 5000
1 1 3 10000
2 1 1 4000
2 1 2 4000
2 1 3 7000

el resultado es el siguiente
numero--mopnrobasc--patente--fecha--hora--tipo--bruto-----eje1----eje2-----eje3--
1 1 xx-9999 xxxx xxx 1 20000 5000 5000 10000
2 1 xx-8888 xxxx xxx 1 15000 4000 4000 7000