• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Mysql > SQLserver 实现分组统计查询(按月、小时分组)

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

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-05

匿名通过本文主要向大家介绍了SQLserver,分组统计等相关知识,希望本文的分享对您有所帮助

首先创建数据表IP地址,访问时间和访问次数。如果每访问一次就插入一条记录,那么AccessCount可以不要,查询时使用count就可以了,这样当访问量很大的时候会对数据库造成很大压力。

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

查询结果如下图:

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 如何获取SqlServer2005表结构(字段,主键,外键,递增,描述)
  • 如何在SQL Server中实现 Limit m,n 的功能
  • 深入Mysql,SqlServer,Oracle主键自动增长的设置详解
  • 小编带你深入解析SQL Server索引的原理
  • sqlserver帐号被禁用如何处理
  • sqlserver查询锁住sql以及解锁的方法
  • MS SQLServer 批量附加数据库的方法
  • SqlServer 2008 创建测试数据的方法
  • 讲解有关sqlserver分页查询处理方法
  • MYSQL同步Sqlserver数据库数据

相关文章

  • 2018-12-05清空所有表中的数据的存储过程
  • 2018-12-05MYSQL解锁和锁表的实例详解
  • 2018-12-05关于优化数据库的10篇文章推荐
  • 2017-05-11解析MySQL数据库性能优化的六大技巧
  • 2017-05-11使用mysql中遇到的几个问题
  • 2018-12-05linux下oracle导入数据的方法
  • 2018-12-05Mysql+Tomcat建立Docker多容器连接的方法
  • 2017-05-11浅谈mysql的中文乱码问题
  • 2018-12-05mysql常用数据库查询
  • 2018-12-05MS SQL Server和MySQL区别

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • MySQL中key与index详细介绍
    • mysql 查询重复的数据的SQL优化方案
    • MySQL主从复制的原理及配置方法(比较详细)
    • MySQL 随机密码生成代码
    • mysql与apt-get在ubuntu下卸载和安装
    • MySQL数据表字段内容的批量修改、清空、复制等更新命令
    • MySQL的事务隔离级别实例教程
    • sql2000挂起无法安装的问题的解决方法
    • MySQL启动时报“The server quit without updating PID file”错误解决办法
    • Mysql子查询IN中使用LIMIT应用示例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有