Ver Mensaje Individual
  #2 (permalink)  
Antiguo 27/10/2011, 14:39
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, 9 meses
Puntos: 774
Respuesta: Organizar períodos por bimestre

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. tipocanal VARCHAR(20),
  4. periodo VARCHAR(10),
  5. vol INT
  6. )
  7. INSERT INTO #temp VALUES ('Super','Ago',7)
  8. INSERT INTO #temp VALUES ('Hiper','Ago',12)
  9. INSERT INTO #temp VALUES ('Mercal','Ago',25)
  10. INSERT INTO #temp VALUES ('Super','Sep',15)
  11. INSERT INTO #temp VALUES ('Hiper','Sep',32)
  12. INSERT INTO #temp VALUES ('Mercal','Sep',18)
  13. INSERT INTO #temp VALUES ('Super','Oct',41)
  14. INSERT INTO #temp VALUES ('Hiper','Oct',20)
  15. INSERT INTO #temp VALUES ('Mercal','Oct',7)
  16. INSERT INTO #temp VALUES ('Super','Nov',15)
  17. INSERT INTO #temp VALUES ('Hiper','Nov',21)
  18. INSERT INTO #temp VALUES ('Mercal','Nov',9)
  19.  
  20.  
  21. SELECT tipocanal,SUM(isnull(bimestre1,0)) Bimestre1 ,SUM(isnull(bimestre2,0)) Bimestre2,SUM(isnull(bimestre3,0)) Bimestre3,
  22. SUM(isnull(bimestre4,0)) Bimestre4,SUM(isnull(bimestre5,0))Bimestre5,SUM(isnull(bimestre6,0))Bimestre6
  23. FROM
  24. (
  25. SELECT
  26. tipocanal,
  27. CASE WHEN periodo='Ene' OR periodo='Feb' THEN SUM(vol) END AS Bimestre1,
  28. CASE WHEN periodo='Mar' OR periodo='Abr' THEN SUM(vol) END AS Bimestre2,
  29. CASE WHEN periodo='May' OR periodo='Jun' THEN SUM(vol) END AS Bimestre3,
  30. CASE WHEN periodo='Jul' OR periodo='Ago' THEN SUM(vol) END AS Bimestre4,
  31. CASE WHEN periodo='Sep' OR periodo='Oct' THEN SUM(vol) END AS Bimestre5,
  32. CASE WHEN periodo='Nov' OR periodo='Dic' THEN SUM(vol) END AS Bimestre6
  33. FROM #temp
  34. GROUP BY tipocanal,periodo
  35. ) t1 GROUP BY tipocanal
  36.  
  37. Resultado:
  38.  
  39. tipocanal   Bimestre1   Bimestre2   Bimestre3   Bimestre4   Bimestre5   Bimestre6
  40. Hiper   0   0   0   12  52  21
  41. Mercal  0   0   0   25  25  9
  42. Super   0   0   0   7   56  15

Si me pudieras ayudar con minimo un gracias mucho te lo agradeceria :)
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me