SQLserver 实现分组统计查询(按月、小时分组)
发布时间:2024-01-24 12:52:15
标签:SQLserver,分组统计
设置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 '
查询结果如下图:


猜你喜欢
- 代码如下:<SCRIPT LANGUAGE="JavaScript"> <!-- //说明:这里用了M
- 前言:我们在做自动化测试的时候,大家都是希望自己写的代码越简洁越好,代码重复量越少越好。那么,我们可以考虑将request的请求类型(如:G
- 非常好的边框样式设置工具,使用该工具您可以很方便的为DIV设置简单的边框样式,如果放在DW中会更好。会制作DW插件的高手,请帮忙制作成DW插
- 本文为大家分享了mysql 8.0安装配置方法,供大家参考,具体内容如下直接使用apt install mysql-server安装,那么恭
- 本文实例讲述了mysql存储过程之创建(CREATE PROCEDURE)和调用(CALL)及变量创建(DECLARE)和赋值(SET)操作
- 1. OpenCV:模板匹配。 获得小跳棋中心位置2.
- 简而言之,channel维护了一个带指针的接受和发送的队列,其中包含mutex锁保证并发安全,数据类型,元素个数,元素大小,channel状
- 报错:D:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py:4
- 前言前面一直使用命令行运行pytest用例,本篇来学下使用pytest.main()来运行测试用例pytest.main()args 传一个
- 一,JS对象<!DOCTYPE html><html><head><meta charset=&q
- Wingdings字体,Symbol字体<html> <head> <title>
- 背景索引是把 * 剑,在提升查询速度的同时会减慢DML的操作。毕竟,索引的维护需要一定的成本。所以,对于索引,要加上该加的,删除无用的。前者是
- 本文实例讲述了Python有序查找算法之二分法。分享给大家供大家参考,具体如下:二分法是一种快速查找的方法,时间复杂度低,逻辑简单易懂,总的
- Request Payload 和 Form Data 请求头上的参数差别在于:Content-TypeForm DataPost表单请求代
- 对于个人站长来说,如何能使自己的网站与众不同、充满个性,一直是不懈努力的目标。除了尽量提高页面的视觉效
- 最常见的方式就是为字段设置主键或唯一索引,当插入重复数据时,抛出错误,程序终止,但这会给后续处理带来麻烦,因此需要对插入语句做特殊处理,尽量
- 让长字符自动换行 (比如 URL 和 Email地址) 目的:让很长的字符串,能自动换行,但是不要把短的单词从中间断开。方法如下:<s
- 在网站的一些应用中需要提供用户直接打印页面的功能,最明显的就是电子优惠券,商家根据网站提供的模板输入内容,然后生成优惠券页面,用户打印这个页
- 概要基于 golang Gin 框架开发 web 服务时, 需要时不时的 go build , 然后重启服务查看运行结果.go build
- 由于数据文件平时在数据库运行的时候处于使用状态,故当数据库处于打开状态时,管理员是无法重命名数据文件名字的。那么一定要更改这个数据文件的名字