select nombre,Company,isnull([4],0) as 'ABRIL',isnull([2],0) as 'FEBRERO',isnull([9],0) as 'SETIEMBRE', 'TOTAL' = (isnull([4],0)+isnull([2],0)+isnull([9],0)),PROMEDIO = ((isnull([4],0)+isnull([2],0)+isnull([9],0)))/3, 'MAXIMO' = (CASE WHEN isnull([4],0) > isnull([2],0) AND isnull([4],0) > isnull([9],0) THEN isnull([4],0) ELSE CASE WHEN isnull([2],0) > isnull([4],0) AND isnull([2],0) > isnull([4],0) THEN isnull([2],0) ELSE CASE WHEN isnull([9],0) > isnull([4],0) AND isnull([9],0) > isnull([2],0) THEN isnull([9],0) ELSE 0 END END END) from (select e.firstname 'nombre',c.CompanyName 'Company',od.UnitPrice 'CANTIDAD' ,DATEPART(MONTH,O.OrderDate) as DATA_MES from Employees e,Customers c,orders o,[Order Details] od where o.CustomerID = c.CustomerID and o.EmployeeID = e.EmployeeID and od.OrderID = o.OrderID group by e.firstname,c.CompanyName,od.UnitPrice,O.OrderDate ) AS MESTABLE PIVOT( SUM(CANTIDAD)
select nombre,Company,isnull([4],0) as 'ABRIL',isnull([2],0) as 'FEBRERO',isnull([9],0) as 'SETIEMBRE',
'TOTAL' = (isnull([4],0)+isnull([2],0)+isnull([9],0)),PROMEDIO = ((isnull([4],0)+isnull([2],0)+isnull([9],0)))/3,
'MAXIMO' = (CASE WHEN isnull([4],0) > isnull([2],0) AND isnull([4],0) > isnull([9],0) THEN isnull([4],0) ELSE
CASE WHEN isnull([2],0) > isnull([4],0) AND isnull([2],0) > isnull([4],0) THEN isnull([2],0) ELSE
CASE WHEN isnull([9],0) > isnull([4],0) AND isnull([9],0) > isnull([2],0) THEN isnull([9],0) ELSE 0
END END END)
from
(select e.firstname 'nombre',c.CompanyName 'Company',od.UnitPrice 'CANTIDAD'
,DATEPART(MONTH,O.OrderDate) as DATA_MES
from Employees e,Customers c,orders o,[Order Details] od
where
o.CustomerID = c.CustomerID and
o.EmployeeID = e.EmployeeID and
od.OrderID = o.OrderID
group by e.firstname,c.CompanyName,od.UnitPrice,O.OrderDate
) AS MESTABLE
PIVOT(
SUM(CANTIDAD)
FOR DATA_MES IN ([4], [2], [9])
)AS PIV_MES
FOR DATA_MES IN ([4], [2], [9]) )AS PIV_MES NecesitoNecesito hallar el mínimo de estos 3 meses quería hacerlo de la misma manera que halle el máximo?? Alguien sabe otra forma quizás más simplificada de hacerlo?