Ver Mensaje Individual
  #33 (permalink)  
Antiguo 22/11/2013, 10:12
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 18 años, 5 meses
Puntos: 774
Respuesta: Consulta en northwind

One question, why you use @pais a parameter if you don't use the parameter into the function??? the solution was simple, only put money instead to decimal en the function table and everything works fine, but as I know that you can't do that here is the function in the format that you need:

Código SQL:
Ver original
  1. ALTER FUNCTION m_ventaPtos(@pais VARCHAR(500))
  2. RETURNS @monto TABLE (Pais VARCHAR(500),[1996] money,[1997] money,[1998] money)
  3. AS
  4. BEGIN
  5.     INSERT @monto
  6.     SELECT shipcountry AS PAIS, isnull(SUM([1996]),0) AS [1996],isnull(SUM([1997]),0) AS [1997],isnull(SUM([1998]),0) AS [1998] FROM
  7. (
  8.     SELECT
  9.     CASE WHEN datepart(yyyy,OrderDate)=1996 THEN SUM(total) END AS [1996],
  10.     CASE WHEN datepart(yyyy,OrderDate)=1997 THEN SUM(total) END AS [1997],
  11.     CASE WHEN datepart(yyyy,OrderDate)=1998 THEN SUM(total) END AS [1998],
  12.     ShipCountry
  13.      FROM(
  14.     SELECT
  15.     OrderDate,(UnitPrice * Quantity) AS total, ShipCountry
  16.      FROM [ORDER Details] AS t1
  17.     LEFT JOIN Orders AS t2 ON (t1.OrderID=t2.OrderID)
  18.     ) AS t1
  19.     GROUP BY ShipCountry,orderdate
  20. ) AS t2 GROUP BY ShipCountry
  21.     RETURN
  22. END
  23.  
  24. SELECT * FROM m_ventaPtos('Argentina')

Reagrds,
Libras
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me