I have a security log table with 4 cols:
UserID, LOGINDate, LOGINTime, ClickEvents
Now I am trying to get a hourly traffic table for past 7 days, which is like:
DAY1 | DAY2 | DAY3 | DAY4 |.... |DAY7
1 0 | 1 | 12 | 4567 | | 43
2
3
4
5
:
:
24
can you show me or give me some idea how to make this table by using SQL?
marc_s Thanks for your quick reply. what I have now is :
select LOGINDate, SUBSTRING(LOGINTime, 1, 2) as 'HoTime', COUNT( *)
From SECLOG
where (CONVERT(varchar( 8) , GETDATE()-7, 112) <= LOGINDate)
group by LOGINDate, SUBSTRING(LOGINTime, 1, 2)
order by LOGINDate, HoTime
which produces me a table like
DATE | HoTime | No of
0926 | 1 | 2
0926 | 2 | 4
0926 | 14 | 6
also it skips the hour without no data.
First do a query that groups by date and hour summing the click events for each hour for the last 7 days.
Then you need to pivot that result so you get the days as a columns. That can be done with a group by on hour and using a case statement in the field list testing for the day number.
To get zero as a value when there are no events in an hour you can use a numbers table that return 1-24 and left join your result set.
Something like this for SQL Server 2008 where you have the date data type.
Try it on SE Data