SQLserver 實(shí)現(xiàn)分組統(tǒng)計(jì)查詢(按月、小時(shí)分組)
設(shè)置AccessCount字段可以根據(jù)需求在特定的時(shí)間范圍內(nèi)如果是相同IP訪問(wèn)就在AccessCount上累加。
Create table Counter
(
CounterID int identity(1,1) not null,
IP varchar(20),
AccessDateTime datetime,
AccessCount int
)
該表在這兒只是演示使用,所以只提供了最基本的字段
現(xiàn)在往表中插入幾條記錄
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 根據(jù)年來(lái)查詢,以月為時(shí)間單位
通常情況下一個(gè)簡(jiǎn)單的分組就能搞定
select
convert(varchar(7),AccessDateTime,120) as Date,
sum(AccessCount) as [Count]
from
Counter
group by
convert(varchar(7),AccessDateTime,120)
像這樣分組后沒(méi)有記錄的月份不會(huì)顯示,如下:

這當(dāng)然不是我們想要的,所以得換一種思路來(lái)實(shí)現(xiàn),如下:
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
查詢結(jié)果如下:

2 根據(jù)天來(lái)查詢,以小時(shí)為單位。這個(gè)和上面的類(lèi)似,代碼如下:
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 '
查詢結(jié)果如下圖:

- sql server遞歸子節(jié)點(diǎn)、父節(jié)點(diǎn)sql查詢表結(jié)構(gòu)的實(shí)例
- SQL Server 樹(shù)形表非循環(huán)遞歸查詢的實(shí)例詳解
- 使用SqlServer CTE遞歸查詢處理樹(shù)、圖和層次結(jié)構(gòu)
- 使用SQLSERVER 2005/2008 遞歸CTE查詢樹(shù)型結(jié)構(gòu)的方法
- SQLSERVER2005 中樹(shù)形數(shù)據(jù)的遞歸查詢
- SQLserver2008使用表達(dá)式遞歸查詢
- 高效的SQLSERVER分頁(yè)查詢(推薦)
- SQL Server SQL高級(jí)查詢語(yǔ)句小結(jié)
- Sql server2005 優(yōu)化查詢速度50個(gè)方法小結(jié)
- sqlserver 模糊查詢常用方法
- sql server實(shí)現(xiàn)遞歸查詢的方法示例
相關(guān)文章
sqlserver 中一些??吹闹笜?biāo)和清除緩存的方法
sqlserver 中一些??吹闹笜?biāo)和清除緩存的方法2009-07-07SQL中查找某幾個(gè)字段完全一樣的數(shù)據(jù)
本文分享SQL語(yǔ)句實(shí)現(xiàn)表中字段的組合累加排序的實(shí)例代碼,希望能給大家做一個(gè)參考。2016-06-06sqlserver 使用SSMS運(yùn)行sql腳本的六種方法
這篇文章主要介紹了sqlserver 使用SSMS運(yùn)行sql腳本的六種方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05SQL Server 2005作業(yè)設(shè)置定時(shí)任務(wù)
這篇文章主要介紹了SQL Server 2005作業(yè)設(shè)置定時(shí)任務(wù)的相關(guān)詳細(xì)步驟,需要的朋友可以參考下2017-01-01MSSQL數(shù)據(jù)庫(kù)的定期自動(dòng)備份計(jì)劃。
MSSQL數(shù)據(jù)庫(kù)的定期自動(dòng)備份計(jì)劃。...2006-08-08sql server創(chuàng)建臨時(shí)表的兩種寫(xiě)法和刪除臨時(shí)表
這篇文章主要介紹了sql server創(chuàng)建臨時(shí)表的兩種寫(xiě)法和刪除臨時(shí)表 ,需要的朋友可以參考下2015-07-07Mybatis4 之Mybatis動(dòng)態(tài)sql的實(shí)現(xiàn)代碼
這篇文章主要介紹了Mybatis4 之Mybatis動(dòng)態(tài)sql的實(shí)現(xiàn)代碼,本文給大家提到了靜態(tài)sql與動(dòng)態(tài)sql有什么區(qū)別,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Blazor Server 應(yīng)用程序中進(jìn)行 HTTP 請(qǐng)求
這篇文章主要介紹了Blazor Server 應(yīng)用程序中進(jìn)行 HTTP 請(qǐng)求方法的相關(guān)資料,感興趣的朋友一起來(lái)學(xué)習(xí)吧2021-08-08