Ver Mensaje Individual
  #6 (permalink)  
Antiguo 15/08/2014, 07:49
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, 3 meses
Puntos: 774
Respuesta: Sumar horas con criterio en comun

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. campo1 VARCHAR(20),
  4. campo2 TIME--varchar(20)
  5. )
  6.  
  7. INSERT INTO #temp VALUES ('S_YOMBLON','08:25:15')
  8. INSERT INTO #temp VALUES ('S_YOMBLON','07:58:30')
  9. INSERT INTO #temp VALUES ('S_YOMBLON','07:55:00')
  10. INSERT INTO #temp VALUES ('S_YOMBLON','07:45:25')
  11. INSERT INTO #temp VALUES ('S_YOMBLON','07:34:50')
  12. INSERT INTO #temp VALUES ('S_YOMBLON','07:24:30')
  13. INSERT INTO #temp VALUES ('S_YOMBLON','08:09:45')
  14. INSERT INTO #temp VALUES ('S_YOMBLON','07:29:38')
  15. INSERT INTO #temp VALUES ('S_YOMBLON','08:50:20')
  16. INSERT INTO #temp VALUES ('S_YOMBLON','08:26:40')
  17. INSERT INTO #temp VALUES ('S_YOMBLON','10:00:43')
  18. INSERT INTO #temp VALUES ('S_YOMBLON','09:50:50')
  19. INSERT INTO #temp VALUES ('S_YOMBLON','08:12:35')
  20. INSERT INTO #temp VALUES ('S_YOMBLON','07:54:17')
  21. INSERT INTO #temp VALUES ('S_YOMBLON','07:55:50')
  22. INSERT INTO #temp VALUES ('S_YOMBLON','07:43:00')
  23. INSERT INTO #temp VALUES ('S_YOMBLON','00:04:55')
  24. INSERT INTO #temp VALUES ('S_YOMBLON','09:16:25')
  25. INSERT INTO #temp VALUES ('S_YOMBLON','08:49:35')
  26. INSERT INTO #temp VALUES ('S_YOMBLON','09:13:43')
  27. INSERT INTO #temp VALUES ('S_YOMBLON','08:12:15')
  28. INSERT INTO #temp VALUES ('S_YOMBLON','08:10:35')
  29. INSERT INTO #temp VALUES ('S_YOMBLON','09:44:37')
  30. INSERT INTO #temp VALUES ('S_YOMBLON','08:24:15')
  31. INSERT INTO #temp VALUES ('S_YOMBLON','01:16:55')
  32. INSERT INTO #temp VALUES ('S_YOMBLON','07:43:25')
  33. INSERT INTO #temp VALUES ('S_YOMBLON','07:56:28')
  34. INSERT INTO #temp VALUES ('S_YOMBLON','07:41:30')
  35. INSERT INTO #temp VALUES ('S_YOMBLON','07:10:15')
  36. INSERT INTO #temp VALUES ('S_YOMBLON','07:12:45')
  37. INSERT INTO #temp VALUES ('S_YOMBLON','08:14:55')
  38. INSERT INTO #temp VALUES ('S_YOMBLON','07:31:58')
  39.  
  40.  
  41. SELECT CAST(HVal AS VARCHAR(10))+':'+RIGHT(MVal+100, 2)+':'+RIGHT(SVal+100, 2)
  42. FROM
  43. (
  44. SELECT H.Val AS hval, M.Val AS mval, S.Val AS sval
  45. FROM (
  46.        --Your query goes here
  47.        SELECT dateadd(SECOND, SUM(datediff(SECOND, 0, campo2)), 0) AS sumtime
  48.        FROM #temp
  49.      ) AS T
  50.   CROSS apply (SELECT datedifF(HOUR, 0, T.sumTime)) AS H(Val)
  51.   CROSS apply (SELECT datediff(MINUTE, 0, dateadd(HOUR, -H.Val, T.sumTime))) AS M(Val)
  52.   CROSS apply (SELECT datediff(SECOND, 0, dateadd(HOUR, -H.Val, dateadd(MINUTE, -M.Val, T.sumTime)))) AS S(Val)
  53.  
  54. ) AS t1

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