SQL游標操作每隔5分鍾時間段數據統計信息


Tb_People 表信息:

         id     uname    era     amount    plushtime          
1000031    張亮    中年    100000    201404050908  
1000032    張亮    中年    100000    201404050913   
1000033    天天    年輕    233233    201404050918   
1000034    天天    年輕    233233    201404050923   
1000035    kimi    年輕    455631    201404050933   
1000036    kimi    年輕    455631    201404050938   

--分析出每5分鍾內有多少人乘車
  --時間轉化秒不用轉化 初始刷卡時間值是20140404090806
   UPDATE [Tb_People] SET shuacardtime=SUBSTRING(plushtime,1,4)+'-'+SUBSTRING(plushtime,5,2)+'-'+SUBSTRING(plushtime,7,2)+' '+SUBSTRING(plushtime,9,2)+':'+SUBSTRING(plushtime,11,2)

結果為:

       id      uname   era    amount    plushtime           shuacardtime
1000031    張亮    中年    100000    201404050908    2014-04-05 09:08
1000032    張亮    中年    100000    201404050913    2014-04-05 09:13
1000033    天天    年輕    233233    201404050918    2014-04-05 09:18
1000034    天天    年輕    233233    201404050923    2014-04-05 09:23
1000035    kimi    年輕    455631    201404050933    2014-04-05 09:33
1000036    kimi    年輕    455631    201404050938    2014-04-05 09:38

--構造區間段
declare @dayBegin datetime,@dayEnd datetime
declare @table table(StartTime datetime,EndTime datetime)
set @dayBegin = '2014-04-05 6:00'
set @dayEnd = '2014-04-06 0:00'
while @dayBegin <=@dayEnd
 begin
 insert @table select @dayBegin,dateadd(mi,5,@dayBegin)  --每5分鍾一個間隔
 set @dayBegin=dateadd(mi,5,@dayBegin)
 end
select * from @table -- 執行后數據如下

 StartTime                              EndTime
2014-04-05 06:00:00.000    2014-04-05 06:05:00.000
2014-04-05 06:05:00.000    2014-04-05 06:10:00.000
2014-04-05 06:10:00.000    2014-04-05 06:15:00.000
2014-04-05 06:15:00.000    2014-04-05 06:20:00.000
2014-04-05 06:20:00.000    2014-04-05 06:25:00.000

--區間段分好了,就可以想到每取出一個時間段,然后在乘車時間記錄表里查詢有多少條記錄在該段時間內就行了,可以考慮用游標。
declare s cursor  --declare 創建游標
static
for select StartTime,EndTime from @table

--定義變量
declare @StartTime datetime,@EndTime datetime
declare @TempTable table(StartTime datetime,EndTime datetime,Number int)

open s  --打開游標
fetch next from s into @StartTime,@EndTime --提取上次提取行的下一行
while(@@fetch_status = 0)
begin
  insert @TempTable select isnull(max(@StartTime),@StartTime),isnull(max(@EndTime),@EndTime), count(*) from Tb_People where shuacardtime > @StartTime and shuacardtime <=@EndTime 
--這里就不能用between and了,不然分隔的時間點上車的人數會在相鄰的兩個區間段重復計數,另外第一班車的上車時間等於@StartTime 沒有計進去,這里不影響總體分析,當然可以做個標記,讀一個區間段時用between...and...就可以了
   fetch next from s into @StartTime,@EndTime
end
close s  --關閉游標
deallocate s  --刪除游標,釋放資源
select * from @TempTable

 執行結果如下:

StartTime                                EndTime                      Number
2014-04-05 09:05:00.000    2014-04-05 09:10:00.000    1
2014-04-05 09:10:00.000    2014-04-05 09:15:00.000    1
2014-04-05 09:15:00.000    2014-04-05 09:20:00.000    1
2014-04-05 09:20:00.000    2014-04-05 09:25:00.000    1
2014-04-05 09:25:00.000    2014-04-05 09:30:00.000    0
2014-04-05 09:30:00.000    2014-04-05 09:35:00.000    1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM