Insert Missing Hours for chart

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 1
Reputation: ffgordy is an unknown quantity at this point 
Solved Threads: 0
ffgordy ffgordy is offline Offline
Newbie Poster

Insert Missing Hours for chart

 
0
  #1
Dec 16th, 2008
We are building a query to count the number of events per hour, per day. Most days there are hours that do not have any activity and therefore where the query is run the count of activities per hour show up but there are gaps and the query excludes these. We still want to show the hours that do not have activity and display a zero so that zero value can then be charted. The query we using looks like this …

  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 = ‘4162AND 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
Last edited by peter_budo; Dec 17th, 2008 at 8:14 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster

Re: Insert Missing Hours for chart

 
0
  #2
Dec 22nd, 2008
You cannot use just select command.
You must use cursor to list the time list.
Try and modify code below:

  1. SET nocount ON
  2. declare @CounterDate smalldatetime,
  3. @MaxDate smalldatetime
  4.  
  5. CREATE TABLE #tmpAct (
  6. ActDate smalldatetime,
  7. Act varchar(255)
  8. )
  9.  
  10. CREATE TABLE #tmpChart (
  11. StartTime smalldatetime
  12. )
  13.  
  14. INSERT #tmpAct (ActDate, Act)
  15. SELECT '2008/12/22 08:15:00', 'Arrive'
  16. union ALL
  17. SELECT '2008/12/22 12:00:00', 'Out'
  18. union ALL
  19. SELECT '2008/12/22 13:00:00', 'In'
  20. union ALL
  21. SELECT '2008/12/22 16:35:00', 'Go home'
  22.  
  23. SELECT @CounterDate = '2008/12/22 00:00:00'
  24. SELECT @MaxDate = '2008/12/22 23:59:00'
  25.  
  26. while @CounterDate < @MaxDate
  27. begin
  28. INSERT #tmpChart (StartTime) select @CounterDate
  29. SELECT @CounterDate = dateadd(hh, 1, @CounterDate)
  30. end
  31.  
  32. SELECT *
  33. FROM #tmpChart C left join #tmpAct A on
  34. A.ActDate >= C.StartTime AND
  35. A.ActDate < dateadd(hh, 1, C.StartTime)
  36.  
  37. DROP TABLE #tmpChart
  38. DROP TABLE #tmpAct
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC