Ver Mensaje Individual
  #4 (permalink)  
Antiguo 21/03/2013, 14:07
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 8 meses
Puntos: 774
Respuesta: consulta para acumular datos

prueba con esto:

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. vendedor VARCHAR(5),
  4. factura INT,
  5. interno VARCHAR(3)
  6. )
  7. DELETE FROM #temp
  8. INSERT INTO #temp VALUES ('V1' ,1 ,'si')
  9. INSERT INTO #temp VALUES ('V2' ,2 ,'si')
  10.  
  11. INSERT INTO #temp VALUES ('V3' ,4 ,'no')
  12. INSERT INTO #temp VALUES ('V3' ,3 ,'no')
  13. INSERT INTO #temp VALUES ('V4' ,1 ,'si')
  14. INSERT INTO #temp VALUES ('V5' ,5 ,'no')
  15.  
  16.  
  17. SELECT vendedor,total FROM #temp,
  18. (
  19. SELECT SUM(factura) total FROM #temp WHERE interno='si') AS t1 WHERE #temp.interno='si'
  20. UNION
  21. SELECT vendedor,SUM(factura) AS total FROM #temp WHERE interno='no' GROUP BY vendedor

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