The Table(Log Analyzer) structure is :
I want to draw a line graph displaying the number of Sessions for last 15 minute to the current time (every minute). I want to write a query which displays two columns with the following information:
- Date:Hour:Minute
- Number of Sessions
I have tried to write a sample query displaying requests per hour:
select convert(nvarchar(16), L.TimeLog, 120) requestTime ,(select Count(SessionID) from LogData where TimeLog < convert(nvarchar(16), TimeLog, 120) and TimeLog > DATEADD (mi , -15 , convert(nvarchar(16), TimeLog, 120) ) ) AS Sessions
from LogData L
group by convert(nvarchar(16), TimeLog, 120) order by requestTime;
I tried to create a query similar to above but it shows wrong results . It should display a number of sessions every 15 minutes . and the group by clause will remain the same (ie. every minute) For example : at 10:00:00 it should represent the number of sessions (sessionID) in last 15 minutes to 10:00:00 ie: 9:45:00 to 10:00:00 and so on
This one gets a little complicated. First you need to create a TimeSlot table that breaks a day into 15 minute chunks.
CREATE TABLE [TimeSlot](
[ID] [int] IDENTITY(1,1) NOT NULL,
[BeginTime] [datetime] NULL,
[EndTime] [datetime] NULL
) ON [PRIMARY]
To fill the table:
DECLARE @Start DATETIME
SET @Start='00:00:00'
WHILE (@Start <'23:59:00')
BEGIN
INSERT INTO TimeSlot(BeginTime, EndTime) values (@Start,dateadd(mi,15,@Start))
SET @Start=dateadd(mi,15,@Start)
END
Then, for the ugly SQL:
SELECT convert(nvarchar(8), L.TimeLog,112) + ' ' + convert(nvarchar(8), S.BeginTime, 108) AS SlotBeginTime ,Count(LogID) AS Sessions
FROM LogData L, TimeSlot S
WHERE (convert(nvarchar(8), L.TimeLog,112) + convert(nvarchar(8), L.TimeLog, 108)) >= (convert(nvarchar(8), L.TimeLog,112)+ convert(nvarchar(8), S.BeginTime,108))
AND (convert(nvarchar(8), L.TimeLog,112) + convert(nvarchar(8), L.TimeLog, 108)) < (convert(nvarchar(8), L.TimeLog,112)+ convert(nvarchar(8), S.EndTime,108))
GROUP BY convert(nvarchar(8), L.TimeLog,112) + ' ' + convert(nvarchar(8), S.BeginTime, 108)
ORDER BY SlotBeginTime;
Try it out. I didn't check how well it works across multiple dates but it does would for a single date.