简体   繁体   English

sql表按小时选择

[英]sql table select by hour

I have a security log table with 4 cols: 我有一个带有4个列的安全日志表:

UserID, LOGINDate, LOGINTime, ClickEvents

Now I am trying to get a hourly traffic table for past 7 days, which is like: 现在,我试图获取过去7天的每小时流量表,如下所示:

   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? 您能告诉我还是让我知道如何使用SQL制作此表?


marc_s Thanks for your quick reply. marc_s感谢您的快速答复。 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. 首先执行一个查询,该查询按日期和小时分组,汇总过去7天每小时的点击事件。
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. 要在一个小时内没有任何事件时获取零值,可以使用返回1-24并保留结果集的数字表。

Something like this for SQL Server 2008 where you have the date data type. 对于SQL Server 2008,您具有日期数据类型。

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 SE数据上尝试

If you're using MS SQL, you could use something like this: 如果您使用的是MS SQL,则可以使用以下内容:

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. 只需将[somedate]替换为相关日期。

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

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. 说1,2 ... 24 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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM