code_junkie

Insert Dates in the return from a query where there is none

Nov 13th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. select datepart(Year, dev_time) as Year,
  2. datepart(Month, dev_time) as Month,
  3. datepart(Day, dev_time) as Day,
  4. datepart(Hour, dev_time) as Hour,
  5. count(tdm_msg) as Total_ACTIVITES
  6. from TCKT_ACT
  7. where tdm_msg = ‘4162′ and dev_time >= DATEADD(day, - 1, GETDATE())
  8. group by datepart(Year, dev_time) ,
  9. datepart(Month, dev_time) ,
  10. datepart(Day, dev_time),
  11. datepart(Hour, dev_time)
  12. order by datepart(Year, dev_time) asc,
  13. datepart(Month, dev_time) asc,
  14. datepart(Day, dev_time) asc,
  15. datepart(Hour, dev_time) asc
  16.  
  17. DECLARE @MinDate DATETIME;
  18. SET @MinDate = CONVERT(varchar, GETDATE(), 101);
  19.  
  20. WITH times AS (
  21. SELECT @MinDate as dt, 1 as depth
  22. UNION ALL
  23. SELECT DATEADD(hh, depth, @MinDate), 1 + depth as depth
  24. FROM times
  25. WHERE DATEADD(hh, depth, @MinDate) <= GETDATE())
  26. SELECT DATEPART(YEAR, t.dt) as [Year],
  27. DATEPART(MONTH, t.dt) as [Month],
  28. DATEPART(DAY, t.dt) as [Day],
  29. DATEPART(HOUR, t.dt) as [Hour],
  30. COUNT(tdm_msg) as Total_ACTIVITES
  31. FROM times t
  32. LEFT JOIN (SELECT * FROM TCKT_ACT WHERE tdm_msg = '4162' and dev_time >= @MinDate) a
  33. ON DATEPART(HOUR, t.dt) = DATEPART(HOUR, a.dev_time)
  34. AND MONTH(t.dt) = MONTH(a.dev_time)
  35. AND DAY(t.dt) = DAY(a.dev_time)
  36. AND YEAR(t.dt) = YEAR(a.dev_time)
  37. GROUP BY DATEPART(YEAR, t.dt) ,
  38. DATEPART(MONTH, t.dt) ,
  39. DATEPART(DAY, t.dt),
  40. DATEPART(HOUR, t.dt)
  41. ORDER BY DATEPART(YEAR, t.dt) asc,
  42. DATEPART(MONTH, t.dt) asc,
  43. DATEPART(DAY, t.dt) asc,
  44. DATEPART(HOUR, t.dt) asc
  45. OPTION (MAXRECURSION 0); /* Just in case you want a longer timespan later on... */
  46.  
  47. if exists (select * from dbo.sysobjects where name = 'fn_daterange') drop function fn_daterange;
  48. go
  49.  
  50. create function fn_daterange
  51. (
  52. @MinDate as datetime,
  53. @MaxDate as datetime,
  54. @intval as datetime
  55. )
  56. returns table
  57. --**************************************************************************
  58. -- Procedure: fn_daterange()
  59. -- Author: Ron Savage
  60. -- Date: 12/16/2008
  61. --
  62. -- Description:
  63. -- This function takes a starting and ending date and an interval, then
  64. -- returns a table of all the dates in that range at the specified interval.
  65. --
  66. -- Change History:
  67. -- Date Init. Description
  68. -- 12/16/2008 RS Created.
  69. -- **************************************************************************
  70. as
  71. return
  72. WITH times (startdate, enddate, intervl) AS
  73. (
  74. SELECT @MinDate as startdate, @MinDate + @intval - .0000001 as enddate, @intval as intervl
  75. UNION ALL
  76. SELECT startdate + intervl as startdate, enddate + intervl as enddate, intervl as intervl
  77. FROM times
  78. WHERE startdate + intervl <= @MaxDate
  79. )
  80. select startdate, enddate from times;
  81.  
  82. go
  83.  
  84. startdate enddate intervl
  85. ----------------------- ----------------------- -----------------------
  86. 2008-12-14 10:00:00.000 2008-12-14 10:59:59.997 1900-01-01 01:00:00.000
  87. 2008-12-14 11:00:00.000 2008-12-14 11:59:59.997 1900-01-01 01:00:00.000
  88. 2008-12-14 12:00:00.000 2008-12-14 12:59:59.997 1900-01-01 01:00:00.000
  89. 2008-12-14 13:00:00.000 2008-12-14 13:59:59.997 1900-01-01 01:00:00.000
  90. 2008-12-14 14:00:00.000 2008-12-14 14:59:59.997 1900-01-01 01:00:00.000
  91. 2008-12-14 15:00:00.000 2008-12-14 15:59:59.997 1900-01-01 01:00:00.000
  92. 2008-12-14 16:00:00.000 2008-12-14 16:59:59.997 1900-01-01 01:00:00.000
  93. 2008-12-14 17:00:00.000 2008-12-14 17:59:59.997 1900-01-01 01:00:00.000
  94. 2008-12-14 18:00:00.000 2008-12-14 18:59:59.997 1900-01-01 01:00:00.000
  95. 2008-12-14 19:00:00.000 2008-12-14 19:59:59.997 1900-01-01 01:00:00.000
  96. 2008-12-14 20:00:00.000 2008-12-14 20:59:59.997 1900-01-01 01:00:00.000
  97.  
  98. eventdate eventnote
  99. ----------------------- --------------------
  100. 2008-12-14 10:01:00.000 oo! an event!
  101. 2008-12-14 10:01:00.000 oo! an event!
  102. 2008-12-14 10:01:00.000 oo! an event!
  103. 2008-12-14 10:01:00.000 oo! an event!
  104. 2008-12-14 10:23:00.000 oo! an event!
  105. 2008-12-14 10:23:00.000 oo! an event!
  106. 2008-12-14 10:23:00.000 oo! an event!
  107. 2008-12-14 11:23:00.000 oo! an event!
  108. 2008-12-14 11:23:00.000 oo! an event!
  109. 2008-12-14 11:23:00.000 oo! an event!
  110. 2008-12-14 11:23:00.000 oo! an event!
  111. 2008-12-14 11:23:00.000 oo! an event!
  112. 2008-12-14 14:23:00.000 oo! an event!
  113. 2008-12-14 14:23:00.000 oo! an event!
  114. 2008-12-14 14:23:00.000 oo! an event!
  115. 2008-12-14 19:23:00.000 oo! an event!
  116. 2008-12-14 19:23:00.000 oo! an event!
  117. 2008-12-14 19:23:00.000 oo! an event!
  118. 2008-12-14 19:23:00.000 oo! an event!
  119. 2008-12-14 19:00:00.000 oo! an event!
  120. 2008-12-14 19:00:00.000 oo! an event!
  121. 2008-12-14 19:00:00.000 oo! an event!
  122.  
  123. 22 Row(s) affected
  124.  
  125. select
  126. dr.startdate,
  127. dr.enddate,
  128. count(me.eventdate) as eventcount
  129. from
  130. fn_daterange('12/14/2008 10:00:00', '12/14/2008 20:00:00', '01:00:00' ) dr
  131.  
  132. LEFT OUTER JOIN myevents me
  133. on ( me.eventdate between dr.startdate and dr.enddate)
  134. group by
  135. dr.startdate,
  136. dr.enddate
  137.  
  138.  
  139. startdate enddate eventcount
  140. ----------------------- ----------------------- ----------
  141. 2008-12-14 10:00:00.000 2008-12-14 10:59:59.993 7
  142. 2008-12-14 11:00:00.000 2008-12-14 11:59:59.993 5
  143. 2008-12-14 12:00:00.000 2008-12-14 12:59:59.993 0
  144. 2008-12-14 13:00:00.000 2008-12-14 13:59:59.993 0
  145. 2008-12-14 14:00:00.000 2008-12-14 14:59:59.993 3
  146. 2008-12-14 15:00:00.000 2008-12-14 15:59:59.993 0
  147. 2008-12-14 16:00:00.000 2008-12-14 16:59:59.993 0
  148. 2008-12-14 17:00:00.000 2008-12-14 17:59:59.993 0
  149. 2008-12-14 18:00:00.000 2008-12-14 18:59:59.993 0
  150. 2008-12-14 19:00:00.000 2008-12-14 19:59:59.993 7
  151. 2008-12-14 20:00:00.000 2008-12-14 20:59:59.993 0
  152.  
  153. 11 Row(s) affected
Add Comment
Please, Sign In to add comment