| | |
Insert Missing Hours for chart
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 1
Reputation:
Solved Threads: 0
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)
SELECT datepart(YEAR, dev_time) as YEAR, datepart(MONTH, dev_time) as MONTH, datepart(DAY, dev_time) as DAY, datepart(HOUR, dev_time) as HOUR, count(tdm_msg) as Total_ACTIVITES FROM TCKT_ACT WHERE tdm_msg = ‘4162′ AND dev_time >= DATEADD(DAY, - 1, GETDATE()) GROUP BY datepart(YEAR, dev_time) , datepart(MONTH, dev_time) , datepart(DAY, dev_time), datepart(HOUR, dev_time) ORDER BY datepart(YEAR, dev_time) asc, datepart(MONTH, dev_time) asc, datepart(DAY, dev_time) asc, 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.
•
•
Join Date: Feb 2008
Posts: 42
Reputation:
Solved Threads: 13
You cannot use just select command.
You must use cursor to list the time list.
Try and modify code below:
You must use cursor to list the time list.
Try and modify code below:
MS SQL Syntax (Toggle Plain Text)
SET nocount ON declare @CounterDate smalldatetime, @MaxDate smalldatetime CREATE TABLE #tmpAct ( ActDate smalldatetime, Act varchar(255) ) CREATE TABLE #tmpChart ( StartTime smalldatetime ) INSERT #tmpAct (ActDate, Act) SELECT '2008/12/22 08:15:00', 'Arrive' union ALL SELECT '2008/12/22 12:00:00', 'Out' union ALL SELECT '2008/12/22 13:00:00', 'In' union ALL SELECT '2008/12/22 16:35:00', 'Go home' SELECT @CounterDate = '2008/12/22 00:00:00' SELECT @MaxDate = '2008/12/22 23:59:00' while @CounterDate < @MaxDate begin INSERT #tmpChart (StartTime) select @CounterDate SELECT @CounterDate = dateadd(hh, 1, @CounterDate) end SELECT * FROM #tmpChart C left join #tmpAct A on A.ActDate >= C.StartTime AND A.ActDate < dateadd(hh, 1, C.StartTime) DROP TABLE #tmpChart DROP TABLE #tmpAct
Hence Wijaya
www.ex-Soft.tk
www.ex-Soft.tk
![]() |
Other Threads in the MS SQL Forum
- Previous Thread: Hello brothers!!! your expert advice is needed!! show me the light!!!
- Next Thread: Need help on Replication
| Thread Tools | Search this Thread |





