Ver Mensaje Individual
  #4 (permalink)  
Antiguo 03/08/2012, 15: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: Acumular valores de una columna en una variable

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. periodo INT,
  4. concepto VARCHAR(20),
  5. camtidad INT
  6. )
  7.  
  8.  
  9. INSERT INTO #temp VALUES (201206,'0010',20)
  10. INSERT INTO #temp VALUES (201206,'0020',10)
  11. INSERT INTO #temp VALUES (201205,'0010',2)
  12. INSERT INTO #temp VALUES (201205,'0020',28)
  13.  
  14. DECLARE @cantidad INT
  15. SELECT @cantidad=cantidad FROM(
  16. SELECT CASE WHEN SUM(camtidad) > 60 THEN 60 ELSE SUM(camtidad) END AS cantidad FROM #temp WHERE concepto LIKE '%20%'
  17. ) t1
  18.  
  19. print @cantidad

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