Ver Mensaje Individual
  #6 (permalink)  
Antiguo 17/04/2012, 08:18
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: usar doble group by

Cita:
Iniciado por damimg Ver Mensaje
Traté de hacerlo pero se complica bastante, me parece que está lejos de usar Union!
como que no sale con union ;)


Código SQL:
Ver original
  1. CREATE TABLE #temp(
  2. vendedor VARCHAR(20),
  3. monto INT
  4. )
  5.  
  6. INSERT INTO #temp VALUES ('vendedor1',1000)
  7. INSERT INTO #temp VALUES ('vendedor2',2000)
  8. INSERT INTO #temp VALUES ('vendedor3',3000)
  9. INSERT INTO #temp VALUES ('vendedor4',4000)
  10. INSERT INTO #temp VALUES ('vendedor5',5000)
  11. INSERT INTO #temp VALUES ('vendedor6',6000)
  12. INSERT INTO #temp VALUES ('vendedor7',7000)
  13. INSERT INTO #temp VALUES ('vendedor8',8000)
  14. INSERT INTO #temp VALUES ('vendedor9',9000)
  15. INSERT INTO #temp VALUES ('vendedor10',10000)
  16. INSERT INTO #temp VALUES ('vendedor11',11000)
  17. INSERT INTO #temp VALUES ('vendedor12',12000)
  18. INSERT INTO #temp VALUES ('vendedor13',13000)
  19. INSERT INTO #temp VALUES ('vendedor14',14000)
  20. INSERT INTO #temp VALUES ('vendedor15',15000)
  21.  
  22.  
  23.  
  24. SELECT 'otros' vendedor, SUM(otros) monto FROM(
  25. SELECT CASE WHEN vendedor NOT IN (SELECT top 10 vendedor FROM #temp ORDER BY monto DESC) THEN SUM(monto) END AS otros FROM #temp
  26. GROUP BY vendedor) t1
  27. UNION
  28. SELECT * FROM
  29. (SELECT top 10 vendedor,monto FROM #temp ORDER BY monto DESC)t2

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