婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識(shí)庫 > SQLserver 實(shí)現(xiàn)分組統(tǒng)計(jì)查詢(按月、小時(shí)分組)

SQLserver 實(shí)現(xiàn)分組統(tǒng)計(jì)查詢(按月、小時(shí)分組)

熱門標(biāo)簽:長沙智能外呼系統(tǒng) 電銷機(jī)器人公司 需要哪些牌照 知名電銷機(jī)器人價(jià)格 廣東防封卡外呼系統(tǒng)原理是什么 分享百度地圖標(biāo)注多個(gè)位置 外呼系統(tǒng)改進(jìn) 湖南電腦外呼系統(tǒng)平臺(tái) 地圖標(biāo)注牌 菏澤語音電銷機(jī)器人加盟公司

設(shè)置AccessCount字段可以根據(jù)需求在特定的時(shí)間范圍內(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ù)年來查詢,以月為時(shí)間單位
通常情況下一個(gè)簡單的分組就能搞定
復(fù)制代碼 代碼如下:

select
convert(varchar(7),AccessDateTime,120) as Date,
sum(AccessCount) as [Count]
from
Counter
group by
convert(varchar(7),AccessDateTime,120)

像這樣分組后沒有記錄的月份不會(huì)顯示,如下:

這當(dāng)然不是我們想要的,所以得換一種思路來實(shí)現(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ù)天來查詢,以小時(shí)為單位。這個(gè)和上面的類似,代碼如下:
復(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é)點(diǎn)、父節(jié)點(diǎn)sql查詢表結(jié)構(gòu)的實(shí)例
  • SQL Server 樹形表非循環(huán)遞歸查詢的實(shí)例詳解
  • 使用SqlServer CTE遞歸查詢處理樹、圖和層次結(jié)構(gòu)
  • 使用SQLSERVER 2005/2008 遞歸CTE查詢樹型結(jié)構(gòu)的方法
  • SQLSERVER2005 中樹形數(shù)據(jù)的遞歸查詢
  • SQLserver2008使用表達(dá)式遞歸查詢
  • 高效的SQLSERVER分頁查詢(推薦)
  • SQL Server SQL高級(jí)查詢語句小結(jié)
  • Sql server2005 優(yōu)化查詢速度50個(gè)方法小結(jié)
  • sqlserver 模糊查詢常用方法
  • sql server實(shí)現(xiàn)遞歸查詢的方法示例

標(biāo)簽:珠海 福建 天水 呼和浩特 商洛 西寧 美容院 泉州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQLserver 實(shí)現(xiàn)分組統(tǒng)計(jì)查詢(按月、小時(shí)分組)》,本文關(guān)鍵詞  SQLserver,實(shí)現(xiàn),分組,統(tǒng)計(jì),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQLserver 實(shí)現(xiàn)分組統(tǒng)計(jì)查詢(按月、小時(shí)分組)》相關(guān)的同類信息!
  • 本頁收集關(guān)于SQLserver 實(shí)現(xiàn)分組統(tǒng)計(jì)查詢(按月、小時(shí)分組)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 西畴县| 昌平区| 巴中市| 绥棱县| 西安市| 紫阳县| 东海县| 钦州市| 信丰县| 扶绥县| 资源县| 始兴县| 南召县| 固安县| 体育| 辽阳县| 青龙| 凤凰县| 万安县| 大田县| 松阳县| 原阳县| 三台县| 滦平县| 大姚县| 临洮县| 太和县| 资兴市| 密云县| 星子县| 佛山市| 柞水县| 义乌市| 邹城市| 枣庄市| 津南区| 兰坪| 新邵县| 龙里县| 桦南县| 方山县|