Ver Mensaje Individual
  #4 (permalink)  
Antiguo 08/08/2015, 12:45
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, 4 meses
Puntos: 774
Respuesta: Query SQLserver

Pues si tienes en tu where -1 mes pues te va a mostrar todo lo del mes de julio mas agosto, si nada mas quieres lo de este mes podrias usar:

Código SQL:
Ver original
  1. SELECT
  2.         COUNT(ti.id)AS total_id,
  3.         datepart(MONTH,ti.create_time) AS mes,
  4. SUM(CASE WHEN  DateDiff(MINUTE, ti.create_time, his.nlasttime) BETWEEN 0 AND 240 THEN 1 ELSE 0 END) AS Hasta4HH,
  5. SUM(CASE WHEN  DateDiff(MINUTE, ti.create_time, his.nlasttime) BETWEEN 241 AND 480  THEN 1 ELSE 0 END) AS Hasta8HH,
  6. SUM(CASE WHEN  DateDiff(MINUTE, ti.create_time, his.nlasttime) BETWEEN 481 AND 1440  THEN 1 ELSE 0 END) AS Hasta24HH,
  7. SUM(CASE WHEN  DateDiff(MINUTE, ti.create_time, his.nlasttime) BETWEEN 1441 AND 4320  THEN 1 ELSE 0 END) AS Hasta72HH,
  8. SUM(CASE WHEN  DateDiff(MINUTE, ti.create_time, his.nlasttime) BETWEEN 4321 AND 10080  THEN 1 ELSE 0 END) AS Hasta168HH,
  9. SUM(CASE WHEN  DateDiff(MINUTE, ti.create_time, his.nlasttime) > 10080  THEN 1 ELSE 0 END) AS Mayores168HH
  10.     FROM Otrs.dbo.ticket_history h
  11.  INNER JOIN Otrs.dbo.ticket ti ON h.ticket_id = ti.id
  12.  INNER JOIN Otrs.dbo.ticket_state s ON ti.ticket_state_id = s.id
  13.  LEFT JOIN otrs.dbo.vwTicketHistoryClose his ON ti.id = his.ticket_id    
  14.         WHERE  datepart(MONTH,ti.create_time)>= datepart(MONTH,getdate()) AND
  15. datepart(YEAR,ti.create_time)>= datepart(YEAR,DATEADD(MONTH,-1,getdate()))
  16.         GROUP BY datepart(MONTH,ti.create_time)
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me