SQLserver 實現(xiàn)分組統(tǒng)計查詢(按月、小時分組)
更新時間:2009年06月05日 23:38:13 作者:
首先創(chuàng)建數(shù)據(jù)表IP地址,訪問時間和訪問次數(shù)。如果每訪問一次就插入一條記錄,那么AccessCount可以不要,查詢時使用count就可以了,這樣當(dāng)訪問量很大的時候會對數(shù)據(jù)庫造成很大壓力。
設(shè)置AccessCount字段可以根據(jù)需求在特定的時間范圍內(nèi)如果是相同IP訪問就在AccessCount上累加。
復(fù)制代碼 代碼如下:
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ù)年來查詢,以月為時間單位
通常情況下一個簡單的分組就能搞定
復(fù)制代碼 代碼如下:
select
convert(varchar(7),AccessDateTime,120) as Date,
sum(AccessCount) as [Count]
from
Counter
group by
convert(varchar(7),AccessDateTime,120)
像這樣分組后沒有記錄的月份不會顯示,如下:

這當(dāng)然不是我們想要的,所以得換一種思路來實現(xiàn),如下:
復(fù)制代碼 代碼如下:
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ù)天來查詢,以小時為單位。這個和上面的類似,代碼如下:
復(fù)制代碼 代碼如下:
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é)點、父節(jié)點sql查詢表結(jié)構(gòu)的實例
- SQL Server 樹形表非循環(huán)遞歸查詢的實例詳解
- 使用SqlServer CTE遞歸查詢處理樹、圖和層次結(jié)構(gòu)
- 使用SQLSERVER 2005/2008 遞歸CTE查詢樹型結(jié)構(gòu)的方法
- SQLSERVER2005 中樹形數(shù)據(jù)的遞歸查詢
- SQLserver2008使用表達(dá)式遞歸查詢
- 高效的SQLSERVER分頁查詢(推薦)
- SQL Server SQL高級查詢語句小結(jié)
- Sql server2005 優(yōu)化查詢速度50個方法小結(jié)
- sqlserver 模糊查詢常用方法
- sql server實現(xiàn)遞歸查詢的方法示例
相關(guān)文章
SQL Server提示"選定的用戶擁有對象,所以無法除去該用戶”
今天在幫朋友弄一臺服務(wù)器的時候當(dāng)我需要刪除一個數(shù)據(jù)庫里的用戶時,提示如下錯誤信息: "選定的用戶擁有對象,所以無法除去該用戶" 如何解決呢?2009-04-04
SqlServer 基礎(chǔ)知識 數(shù)據(jù)檢索、查詢排序語句
SqlServer 基礎(chǔ)知識 數(shù)據(jù)檢索、查詢排序語句,需要的朋友可以參考下。2011-10-10
SQL Server并行操作優(yōu)化避免并行操作被抑制而影響SQL的執(zhí)行效率
這篇文章主要介紹了SQL Server并行操作優(yōu)化避免并行操作被抑制而影響SQL的執(zhí)行效率 的相關(guān)資料,需要的朋友可以參考下2016-07-07
SqlServer數(shù)據(jù)庫備份與還原的實現(xiàn)步驟
這篇文章主要介紹了SqlServer數(shù)據(jù)庫備份與還原的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
mssql數(shù)據(jù)庫系統(tǒng)崩潰后的一般處理步驟與方法
mssql數(shù)據(jù)庫系統(tǒng)崩潰后的一般處理步驟與方法...2007-03-03

