user1146865
user1146865

Reputation: 3

sql table select by hour

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.

Upvotes: 0

Views: 1248

Answers (3)

vmvadivel
vmvadivel

Reputation: 1067

This is not a complete solution. I have just provided an idea hoping that based on which you can build your solution. How about creating a table to store Hours information in it? Say, 1,2... 24 in it. Then may be you can do something like this:

SELECT
    h.[Hour],
    SUM(CASE WHEN  t.LOGINDate BETWEEN GetDate() - 1 AND GetDate()      THEN  1 ELSE 0 END) as DAY1,
    SUM(CASE WHEN  t.LOGINDate BETWEEN GetDate() - 2 AND GetDate() - 1  THEN  1 ELSE 0 END) as DAY2,
    SUM(CASE WHEN  t.LOGINDate BETWEEN GetDate() - 3 AND GetDate() - 2  THEN  1 ELSE 0 END) as DAY3, 
    SUM(CASE WHEN  t.LOGINDate BETWEEN GetDate() - 4 AND GetDate() - 3  THEN  1 ELSE 0 END) as DAY4, 
    SUM(CASE WHEN  t.LOGINDate BETWEEN GetDate() - 5 AND GetDate() - 4  THEN  1 ELSE 0 END) as DAY5, 
    SUM(CASE WHEN  t.LOGINDate BETWEEN GetDate() - 6 AND GetDate() - 5  THEN  1 ELSE 0 END) as DAY6, 
    SUM(CASE WHEN  t.LOGINDate BETWEEN GetDate() - 7 AND GetDate() - 6  THEN  1 ELSE 0 END) as DAY7 
FROM 
    tblHour h LEFT JOIN SecurityLog t 
    ON h.[Hour] = DATEPART(hour, t.LOGINDate)
WHERE 
    t.LOGINDate >= CONVERT(datetime, CONVERT(varchar, getdate() -7, 102)) AND
    t.LOGINDate < DATEADD(day, 1, CONVERT(datetime, CONVERT(varchar, getdate(), 102)))
GROUP BY 
    t.LOGINDate, h.[Hour]

Upvotes: 0

Mikael Eriksson
Mikael Eriksson

Reputation: 138960

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.

with C as
(
  select datediff(day, LoginDate, getdate()) as DD, 
         LoginTime,
         sum(ClickEvents) as ClickEvents
  from YourTable
  where LoginDate >= dateadd(day, -7, cast(getdate() as date))
  group by LoginDate, LoginTime
), H as
(
  select 1 as LoginTime
  union all
  select H.LoginTime+1
  from H
  where H.LoginTime < 24
)  
select H.LoginTime, 
       sum(case DD when 1 then C.ClickEvents else 0 end) as Day1,
       sum(case DD when 2 then C.ClickEvents else 0 end) as Day2,
       sum(case DD when 3 then C.ClickEvents else 0 end) as Day3,
       sum(case DD when 4 then C.ClickEvents else 0 end) as Day4,
       sum(case DD when 5 then C.ClickEvents else 0 end) as Day5,
       sum(case DD when 6 then C.ClickEvents else 0 end) as Day6,
       sum(case DD when 7 then C.ClickEvents else 0 end) as Day7
from H
  left outer join C
    on C.LoginTime = H.LoginTime
group by H.LoginTime
order by H.LoginTime;

Try it on SE Data

Upvotes: 1

Demig0d
Demig0d

Reputation: 158

If you're using MS SQL, you could use something like this:

SELECT COUNT(*) FROM SecurityLog WHERE DATEPART(hh, LOGINDate) = 1 AND LOGINDate = [somedate]
SELECT COUNT(*) FROM SecurityLog WHERE DATEPART(hh, LOGINDate) = 2 AND LOGINDate = [somedate]
....
....
SELECT COUNT(*) FROM SecurityLog WHERE DATEPART(hh, LOGINDate) = 24 AND LOGINDate = [somedate]

This would tell you how many records are in the security log that match any given hour, any given day. Just replace [somedate] with the day in question.

Reference: http://msdn.microsoft.com/en-us/library/ms174420.aspx http://syntaxhelp.com/SQLServer/Breaking-a-date-by-hour-into-24-parts

Upvotes: 0

Related Questions