SQLserver 实现分组统计查询(按月、小时分组)

所属分类: 数据库 / MsSql 阅读数: 518
收藏 0 赞 0 分享

设置AccessCount字段可以根据需求在特定的时间范围内如果是相同IP访问就在AccessCount上累加。

复制代码 代码如下:

Create table Counter
(
CounterID int identity(1,1) not null,
IP varchar(20),
AccessDateTime datetime,
AccessCount int
)

该表在这儿只是演示使用,所以只提供了最基本的字段
现在往表中插入几条记录
insert into Counter
select '127.0.0.1',getdate(),1 union all
select '127.0.0.2',getdate(),1 union all
select '127.0.0.3',getdate(),1

1 根据年来查询,以月为时间单位
通常情况下一个简单的分组就能搞定
复制代码 代码如下:

select
convert(varchar(7),AccessDateTime,120) as Date,
sum(AccessCount) as [Count]
from
Counter
group by
convert(varchar(7),AccessDateTime,120)

像这样分组后没有记录的月份不会显示,如下:

这当然不是我们想要的,所以得换一种思路来实现,如下:
复制代码 代码如下:

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 by
m

查询结果如下:

2 根据天来查询,以小时为单位。这个和上面的类似,代码如下:
复制代码 代码如下:

declare @DateTime datetime
set @DateTime=getdate()
select
right(100+a,2)+ ':00 -> '+right(100+b,2)+ ':00 ' as DateSpan,
sum(
case when datepart(hour,AccessDateTime)> =a
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 '

查询结果如下图:

更多精彩内容其他人还在看

SQL Server正确删除Windows认证用户的方法

这篇文章主要给大家介绍了关于SQL Server正确删除Windows认证用户的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用SQL Server具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

SQL Server查看login所授予的具体权限问题

在SQL Server数据库中如何查看一个登录名(login)的具体权限呢,下面脚本之家小编给大家带来了SQL Server查看login所授予的具体权限问题,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Mysql8.0.17安装教程【推荐】

本文通过图文并茂的形式给大家介绍了Mysql8.0.17安装,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C# ling to sql 取多条记录最大时间

这篇文章主要介绍了C# ling to sql 取多条记录最大时间,文中通过实例代码给大家介绍了sql 查询相同记录下日期最大的 一条,代码简单易懂,需要的朋友可以参考下
收藏 0 赞 0 分享

sql server编写archive通用模板脚本实现自动分批删除数据

这篇文章主要介绍了sql server编写archive通用模板脚本实现自动分批删除数据,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server怎么找出一个表包含的页信息(Page)

这篇文章主要给大家介绍了关于SQL Server是如何找出一个表包含的页信息(Page)的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用SQL Server具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

SQLyog连接MySQL8.0报2058错误的完美解决方法

这篇文章主要介绍了SQLyog连接MySQL8.0报2058错误的完美解决方法,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL SERVER日志进行收缩的图文教程

这篇文章主要给大家介绍了关于SQL SERVER日志进行收缩的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用SQL SERVER具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

SqlServer中批量update语句

我现在想把S_USER表中的ACCOUNT批量修改成S_PERSON的ACCOUNT,那么就可以参考下面的语句,要找到对应的字段
收藏 0 赞 0 分享

SqlServer AS的用法

本篇文章简要分析,在SQL SERVER中,AS的基本用法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多