Ver Mensaje Individual
  #2 (permalink)  
Antiguo 14/09/2011, 15:17
Avatar de Andres95
Andres95
Colaborador
 
Fecha de Ingreso: diciembre-2004
Mensajes: 1.802
Antigüedad: 19 años, 5 meses
Puntos: 38
Respuesta: Como dividir valores de esta tabla

Código:
if OBJECT_ID ('tempdb..#temp') is not null drop table #temp

CREATE TABLE #temp (
    cate int 
    ,valor int
    ,ano int
)

INSERT INTO #temp(cate, valor, ano)
VALUES  (1,100,2010)
        ,(1,500,2011)
        ,(2,200,2010)
        ,(2,600,2011)
        ,(3,300,2010)
        ,(3,700,2011)

SELECT cate, ano, valor FROM #temp WITH (NOLOCK)
--
SELECT cate
     ,[2010]/CAST([2011] AS DECIMAL(7,2)) [SIN REDONDEAR]
     ,CAST(ROUND([2010]/CAST([2011] AS DECIMAL(7,2)),2) AS DECIMAL(7,2) )  [CON DOS DECIMALES]
     ,[2010]/[2011] [Division entera resulta en cero]
FROM #temp p     WITH (NOLOCK)
PIVOT (    
        SUM (valor)    
        FOR ano IN ([2010], [2011] )    
      ) AS pvt
Espero te sirva.. Saludos!
__________________
La sencillez y naturalidad son el supremo y último fin de la cultura...
--
MCTS : SQL Server 2008, .NET Framework 3.5, ASP.NET Applications.