≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 数据库 > SQL Server

SQL Server 分组统计查询(按月、小时分组)

人气:1467 时间:2019-06-09

这篇文章主要为大家详细介绍了SQL Server 分组统计查询(按月、小时分组),具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

设置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 ' 

查询结果如下图:
【图片暂缺】

 

本文来自:http://www.q1010.com/179/7660-0.html

注:关于SQL Server 分组统计查询(按月、小时分组)的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:SQL SERVER

您可能感兴趣的文章

  • SQLServer 触发器 数据库进行数据备份
  • 用户"sa"登陆失败 SQLServer 错误18456的解决方法
  • SQL Server做购物车系统时利用到得几个sqlserver 存储过程
  • SQLServer 优化SQL语句 in 和not in的替代方案
  • win2003 安装 sqlserver 2005的方法
  • SQL Server当恢复sqlserver bak文件时,原始的用户无法删除的解决方法
  • SQL Server分页存储过程(三)在sqlserver中打造更加准确的分页结果
  • 分页存储过程(二)在sqlserver中返回更加准确的分页结果
  • SQL Server 2000数据库同步 同步两个SQLServer数据库的内容
  • SQLServer触发器创建、删除、修改、查看示例代码
上一篇:SQL Server 两种数据插入方式
下一篇:SQL Server 查询正在实行的SQL语句
热门文章
  • SQL Server SQL获取第一条记录的方法
  • SQL Server出现System.OutOfMemoryException异常的解决方法
  • SQL Server的 update from 语句的简单示例
  • SQL Server 数据库备份方法菜鸟教程
  • SQL Server 多表关联时在where语句中慎用trim()方法
  • SQL Server数据类型及长度限制详细说明
  • mybaits非配置原因,导致SqlSession was not registered for synchronization异常解析
  • SQL Server 收缩后对数据库的使用有影响吗?
  • SQL Server 格式导致的Excel导入sql出现异常的解决方法
  • SQL Server 连接服务器出现错误 7391的解决方法
  • 最新文章
    • SQL Server存储过程基本语法的简单示例
    • sql查询时增加自动编号和分页的简单示例
    • sql轻松应付百万数据的高效数据分页存储过程的简单示例
    • sql获取一条数据中所有字段的名称和值的实现方法
    • sql分割函数的简单示例
    • SQL Server异常捕获的简单示例
    • SQL SERVER回滚恢复误操作数据的实现方法
    • SQL Server函数或存储过程中抛出异常的实现方法
    • SQL Server创建数据库的完整代码
    • SQL Server创建数据库的命令用法示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。