943,789 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 1020
  • MS SQL RSS
Dec 16th, 2008
0

Insert Missing Hours for chart

Expand Post »
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 …

sql Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ffgordy is offline Offline
1 posts
since Dec 2008
Dec 22nd, 2008
0

Re: Insert Missing Hours for chart

You cannot use just select command.
You must use cursor to list the time list.
Try and modify code below:

MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 13
Light Poster
huangzhi is offline Offline
48 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Hello brothers!!! your expert advice is needed!! show me the light!!!
Next Thread in MS SQL Forum Timeline: Need help on Replication





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC