You can set the accesscount field to accumulate access from the same IP address in a specific time range as required.
CopyCode The Code is as follows: Create Table counter
(
Counterid int identity (1, 1) not null,
IP varchar (20 ),
Accessdatetime datetime,
Accesscount int
)
This table is only used for demonstration, so only the most basic fields are provided.
Insert several records into the table
Insert into counter
Select '2014. 0.0.1 ', getdate (), 1 Union all
Select '2014. 0.0.2 ', getdate (), 1 Union all
Select '2014. 0.0.3 ', getdate (), 1
1 query by year, in the unit of month
generally, a simple grouping can achieve copy Code the code is as follows: Select
convert (varchar (7 ), accessdatetime, 120) as date,
sum (accesscount) as [count]
from
counter
group by
convert (varchar (7), accessdatetime, 120)
The month that is not recorded after grouping like this will not be displayed, as shown below:
This is of course not what we want, so we have to change the way we implement it, as follows:Copy codeThe Code is as follows: declare @ year int
Set @ year = 2009
Select
M as [date],
Sum (
Case when datepart (month, accessdatetime) = m
Then accesscount else 0 end
) As [count]
From
Counter C,
(
Select 1 m
Union all select 2
Union all select 3
Union all select 4
Union all select 5
Union all select 6
Union all select 7
Union all Select 8
Union all select 9
Union all select 10
Union all select 11
Union all select 12
) AA
Where
@ Year = year (accessdatetime)
Group
M
The query result is as follows:
2. query by day, in hours. This is similar to the above Code. The Code is as follows: Copy code The Code is as follows: declare @ datetime
Set @ datetime = getdate ()
Select
Right (100 + A, 2) + ': 00->' + right (100 + B, 2) + ': 00' as datespan,
Sum (
Case when datepart (hour, accessdatetime)> =
And datepart (hour, accessdatetime) <B
Then accesscount else 0 end
) As [count]
From Counter C,
(Select 0 A, 1 B
Union all select 1, 2
Union all select 2, 3
Union all select 3, 4
Union all select 4, 5
Union all select 5, 6
Union all select 6, 7
Union all select 7,8
Union all Select 8, 9
Union all select 9, 10
Union all select 10, 11
Union all select 11,12
Union all select 12,13
Union all select 13, 14
Union all select 14,15
Union all select 15,16
Union all select 16, 17
Union all select 17,18
Union all select 18, 19
Union all select 19,20
Union all select 20, 21
Union all select 21,22
Union all select 22, 23
Union all select 23,24
) AA
Where datediff (day, @ datetime, accessdatetime) = 0
Group by right (100 + A, 2) + ': 00->' + right (100 + B, 2) + ': 00'
The query result is as follows: