¿Cómo puedo hacer una row final con operaciones sobre valores de la columna?
Hola a tod@s!
Necesito sacar un resultado como este(olvidaos del año)
http://img805.imageshack.us/img805/716/testzz.png
Esta es la query (un poco burda, falta refinarla) para sacar los datos de las 2 primeras rows:
select 'Total C.Center' as prueba, test.Enero, test.Febrero, test.Marzo, test.Abril, test.Mayo, test.Junio, test.Julio, test.Agosto, test.Septiembre, test.Octubre, test.Noviembre, test.Diciembre,
(test.Enero + test.Febrero + test.Marzo + test.Abril + test.Mayo + test.Junio + test.Julio + test.Agosto + test.Septiembre + test.Octubre + test.Noviembre + test.Diciembre) as "ACUM"
from
(select * from (
select cs.case_summary_id as num , extract(MONTH from cs.create_date) as fecha
from case_summary cs
where cs.status = 'CLOS'
and extract (YEAR from cs.create_date) = extract (YEAR from sysdate)
)
PIVOT (count(num) for fecha in
( '1' as Enero,
'2' as Febrero,
'3' as Marzo,
'4' as Abril,
'5' as Mayo,
'6' as Junio,
'7' as Julio,
'8' as Agosto,
'9' as Septiembre,
'10' as Octubre,
'11' as Noviembre,
'12' as Diciembre))) test
UNION ALL
select 'Telef. Inmediato' as prueba, test.Enero, test.Febrero, test.Marzo, test.Abril, test.Mayo, test.Junio, test.Julio, test.Agosto, test.Septiembre, test.Octubre, test.Noviembre, test.Diciembre,
(test.Enero + test.Febrero + test.Marzo + test.Abril + test.Mayo + test.Junio + test.Julio + test.Agosto + test.Septiembre + test.Octubre + test.Noviembre + test.Diciembre) as "ACUM"
from
(select * from (
select cs.case_summary_id as num , extract(MONTH from cs.create_date) as fecha
from case_summary cs
inner join case_summary_dynamic csd on csd.case_summary_id = csd.case_summary_id and csd.table_id = 4888500000152024
where cs.status = 'CLOS' and csd.code_4 = -1
and extract (YEAR from cs.create_date) = extract (YEAR from sysdate)
)
PIVOT (count(num) for fecha in
( '1' as Enero,
'2' as Febrero,
'3' as Marzo,
'4' as Abril,
'5' as Mayo,
'6' as Junio,
'7' as Julio,
'8' as Agosto,
'9' as Septiembre,
'10' as Octubre,
'11' as Noviembre,
'12' as Diciembre))) test
Me gustaría saber como puedo sacar la 3ª row, (que es la segunda row / la primera row * 100) sin tener que hacer otro UNION haciendo otra vez las dos queries para conseguir los datos. Me parece muy ineficiente tener que hacer otras dos request a la DB para operar con datos que ya tengo.
Cualquier idea sería bien recibida.
Muchas Gracias. :-)