分時間段查詢

2021-05-22 08:05:07 字數 4802 閱讀 9846

declare @t table (

時間 datetime,

金額 int

)insert @t select

'2007-1-1 10:00:23',           8

--union all select

'2007-1-1 01:00:04',           4

union all select

'2007-1-1 01:00:14',           4

union all select

'2007-1-1 01:00:16',           4

union all select

'2007-1-1 01:00:24',           4

union all select

'2007-1-1 01:00:34',           4

union all select

'2007-1-1 01:00:44',           4

union all select

'2007-1-1 01:00:54',           4

union all select

'2007-1-1 01:01:24',           4

union all select

'2007-1-1 01:01:34',           4

union all select

'2007-1-1 01:01:44',           4

union all select

'2007-1-1 01:05:00',           2  

union all select

'2007-1-1 01:06:12',           3

union all select

'2007-1-1 01:08:00',           1

union all select

'2007-1-1 01:12:11',           5

--union all select

'2007-1-1 10:00:04',           4

union all select

'2007-1-1 10:00:14',           4

union all select

'2007-1-1 10:00:16',           4

union all select

'2007-1-1 10:00:24',           4

union all select

'2007-1-1 10:00:34',           4

union all select

'2007-1-1 10:00:44',           4

union all select

'2007-1-1 10:00:54',           4

union all select

'2007-1-1 10:01:24',           4

union all select

'2007-1-1 10:01:34',           4

union all select

'2007-1-1 10:01:44',           4

union all select

'2007-1-1 10:05:00',           2  

union all select

'2007-1-1 10:06:12',           3

union all select

'2007-1-1 10:08:00',           1

union all select

'2007-1-1 10:12:11',           5

union all select

'2007-1-1 11:01:24',           4

union all select

'2007-1-1 11:05:00',           2  

union all select

'2007-1-1 11:06:12',           3

union all select

'2007-1-1 11:08:00',           1

union all select

'2007-1-1 11:12:11',           5

union all select

'2007-1-1 11:31:24',           4

union all select

'2007-1-1 11:35:00',           2  

union all select

'2007-1-1 11:36:12',           3

union all select

'2007-1-1 11:48:00',           1

union all select

'2007-1-1 11:52:11',           5

union all select

'2007-1-1 10:31:24',           4

union all select

'2007-1-1 10:35:00',           2  

union all select

'2007-1-1 10:36:12',           3

union all select

'2007-1-1 10:48:00',           1

union all select

'2007-1-1 10:52:11',           5

----按5分鐘分段

--select dateadd(mi,(datediff(mi,convert(varchar(10),時間,112),時間)/5+1)*5,convert(varchar(10),時間,112)) as 時間段,

--count(*) as 行數,sum(金額) as 總金額

--from @t

--group by dateadd(mi,(datediff(mi,convert(varchar(10),時間,112),時間)/5+1)*5,convert(varchar(10),時間,112))

--按10分鐘分段

--select dateadd(mi,(datediff(mi,convert(varchar(10),時間,112),時間)/10+1)*10,convert(varchar(10),時間,112)) as 時間段,

--count(*) as 行數,sum(金額) as 總金額

--from @t

--group by dateadd(mi,(datediff(mi,convert(varchar(10),時間,112),時間)/10+1)*10,convert(varchar(10),時間,112))

--按1小時分段

--select dateadd(mi,(datediff(mi,convert(varchar(10),時間,112),時間)/60+1)*60,convert(varchar(10),時間,112)) as 時間段,

--count(*) as 行數,sum(金額) as 總金額

--from @t

--group by dateadd(mi,(datediff(mi,convert(varchar(10),時間,112),時間)/60+1)*60,convert(varchar(10),時間,112))

--按30秒分段

--select  dateadd(s,(datediff(s,convert(varchar(10),時間,112),時間)/30+1)*30,convert(varchar(10),時間,120)) as 時間段,

--count(*) as 行數,sum(金額) as 總金額

--from @t

--group by dateadd(s,(datediff(s,convert(varchar(10),時間,112),時間)/30+1)*30,convert(varchar(10),時間,120))

----按30秒分段

--select convert( varchar(10),(dateadd(s,(datediff(s,convert(varchar(10),時間,112),時間)/30+1)*30,convert(varchar(10),時間,120))),108) as 時間段,

--count(*) as 行數,sum(金額) as 總金額

--from @t

--group by dateadd(s,(datediff(s,convert(varchar(10),時間,112),時間)/30+1)*30,convert(varchar(10),時間,120))

--按n*60秒分段

select convert( varchar(10),(dateadd(s,(datediff(s,convert(varchar(10),時間,112),時間)/(5*60)+1)*(5*60),convert(varchar(10),時間,120))),108) as 時間段,

count(*) as 行數,sum(金額) as 總金額

from @t

group by dateadd(s,(datediff(s,convert(varchar(10),時間,112),時間)/(5*60)+1)*(5*60),convert(varchar(10),時間,120))

MySql分時間段統計

在統計業務辦理人數的時候,需要從task 201907表裡面統計7月內每一天辦理的人數,網羅上查到大家用格式化時間戳的方法,剛開始還沒看懂,原來是利用的from unixtime函式。查詢邏輯 表中有時間字段,按照時間 天 分組統計參與人數。問題 如果直接按照時間 datetime 字段分組,是精確...

SQL時間段查詢

access的話 select from table where date1 2008 1 20 and date2 2007 8 30 mysql的話 select from table where date1 2008 1 20 and date2 2007 3 30 也可以 select fr...

SQL時間段查詢

sql時間段查詢 access的話 select from table where date1 2008 1 20 and date2 2007 8 30 mysql的話 select from table where date1 2008 1 20 and date2 2007 3 30 也可以 ...