Ver Mensaje Individual
  #6 (permalink)  
Antiguo 30/07/2013, 07:57
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, 3 meses
Puntos: 774
Respuesta: Duda con esta consulta

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. id_mov INT,
  4. fecha datetime,
  5. depo_mov INT,
  6. arti_mov INT,
  7. cant INT,
  8. tipomov INT
  9. )
  10.  
  11.  
  12. INSERT INTO #temp VALUES (1,'2013-23-06 00:00:00',  1,  1,  5, 1)
  13. INSERT INTO #temp VALUES (2,'2013-23-06 00:00:00',1,    1,  1, 2)
  14. INSERT INTO #temp VALUES (3,'2013-23-06 00:00:00',1,    1,  3, 3)
  15. INSERT INTO #temp VALUES (4,'2013-23-06 00:00:00',1,    1, 10, 4)
  16. INSERT INTO #temp VALUES (5,'2013-21-07 00:00:00',2,    1, 3, 5)
  17. INSERT INTO #temp VALUES (6,'2013-22-06 00:00:00',3,    1,  5, 4)
  18. INSERT INTO #temp VALUES (7,'2013-30-07 00:00:00',1,    2,  4, 3)
  19. INSERT INTO #temp VALUES (8,'2013-23-06 00:00:00',2,    2,  1, 4)
  20. INSERT INTO #temp VALUES (9,'2013-23-07 00:00:00',3,    2,  1, 4)
  21. INSERT INTO #temp VALUES (10,'2013-25-06 00:00:00',1,   3,  2, 3)
  22. INSERT INTO #temp VALUES (11,'2012-24-06 00:00:00',1,   1,  3, 2)
  23. INSERT INTO #temp VALUES (12,'2012-24-06 00:00:00',1,   1,  5, 4)
  24. INSERT INTO #temp VALUES (13,'2012-24-06 00:00:00', 1,  1,  1, 2)
  25.  
  26.  
  27.  
  28. --drop table #temp2
  29. CREATE TABLE #temp2
  30. (
  31. id_tipo INT,
  32. factor INT
  33. )
  34.  
  35. INSERT INTO #temp2 VALUES (1, 1)
  36. INSERT INTO #temp2 VALUES (2, -1)
  37. INSERT INTO #temp2 VALUES (3, -1)
  38. INSERT INTO #temp2 VALUES (4, 1 )
  39. INSERT INTO #temp2 VALUES (5, -1)
  40.  
  41. SELECT mes,[año],depo_mov,arti_mov,SUM(factor) total FROM(
  42. SELECT datepart(mm,fecha) AS mes, datepart(yyyy,fecha) AS [año],depo_mov,arti_mov,(cant*factor) AS factor FROM #temp AS t1
  43. LEFT JOIN #temp2 AS t2 ON (t1.tipomov=t2.id_tipo)) t1 GROUP BY mes,[año],depo_mov,arti_mov
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me