sql server表峰值时间
我有一个应用程序,有1000个用户在不同的时间出于不同的目的登录。 现在的任务是以某种方式计算出“高峰时间的用户数” 我们在sql中记录的是userLoginTime,timespunt。 这里的问题是 如何实际计算应用程序的峰值时间。 以及如何计算高峰时间的用户数sql server表峰值时间,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有一个应用程序,有1000个用户在不同的时间出于不同的目的登录。 现在的任务是以某种方式计算出“高峰时间的用户数” 我们在sql中记录的是userLoginTime,timespunt。 这里的问题是 如何实际计算应用程序的峰值时间。 以及如何计算高峰时间的用户数 在Sql中是否可能?我已经做了一个游戏-我正在使用记录的开始和结束日期时间2值的会话,但希望您可以调整当前数据以符合以下要求: 样本数据(如果我的答案错了,也许您可以采用此方法,将其添加到您的问题中,并添加更多样本和预期输出): 以
在Sql中是否可能?我已经做了一个游戏-我正在使用记录的开始和结束日期时间2值的会话,但希望您可以调整当前数据以符合以下要求: 样本数据(如果我的答案错了,也许您可以采用此方法,将其添加到您的问题中,并添加更多样本和预期输出): 以及查询:
--Logically, the highest number of simultaneous users was reached at some point when a session started
;with StartTimes as (
select distinct SessionStart as Instant from #Sessions
), Overlaps as (
select
st.Instant,COUNT(*) as Cnt,MIN(s.SessionEnd) as SessionEnd
from
StartTimes st
inner join
#Sessions s
on
st.Instant >= s.SessionStart and
st.Instant < s.SessionEnd
group by
st.Instant
), RankedOverlaps as (
select Instant as SessionStart,Cnt,SessionEnd,RANK() OVER (ORDER BY Cnt desc) as rnk
from Overlaps
)
select * from RankedOverlaps where rnk = 1
drop table #Sessions
另一种方法仍然使用上述方法,但如果您还想分析“不太峰值”值,则如下所示:
--An alternate approach - arrange all of the distinct time values from Sessions into order
;with Instants as (
select SessionStart as Instant from #Sessions
union --We want distinct here
select SessionEnd from #Sessions
), OrderedInstants as (
select Instant,ROW_NUMBER() OVER (ORDER BY Instant) as rn
from Instants
), Intervals as (
select oi1.Instant as StartTime,oi2.Instant as EndTime
from
OrderedInstants oi1
inner join
OrderedInstants oi2
on
oi1.rn = oi2.rn - 1
), IntervalOverlaps as (
select
StartTime,
EndTime,
COUNT(*) as Cnt
from
Intervals i
inner join
#Sessions s
on
i.StartTime < s.SessionEnd and
s.SessionStart < i.EndTime
group by
StartTime,
EndTime
)
select * from IntervalOverlaps order by Cnt desc,StartTime
这里解释了一个解决方案:
userLoginTime
和timespunt
的数据类型是什么?您正在尝试查找整个集合中登录用户最多的时间间隔?(我说的是间隔,以防出现平局)(时间间隔很可能比任何覆盖时间段的单个用户都小)数据类型分别是DateTime和Integer。记录的时间是sql中的当前日期时间,因此可能是15:24 12:34
SessionStart Cnt SessionEnd rnk
---------------------- ----------- ---------------------- --------------------
2012-01-03 00:00:00.00 2 2012-01-05 00:00:00.00 1
2012-01-07 00:00:00.00 2 2012-01-08 00:00:00.00 1
--An alternate approach - arrange all of the distinct time values from Sessions into order
;with Instants as (
select SessionStart as Instant from #Sessions
union --We want distinct here
select SessionEnd from #Sessions
), OrderedInstants as (
select Instant,ROW_NUMBER() OVER (ORDER BY Instant) as rn
from Instants
), Intervals as (
select oi1.Instant as StartTime,oi2.Instant as EndTime
from
OrderedInstants oi1
inner join
OrderedInstants oi2
on
oi1.rn = oi2.rn - 1
), IntervalOverlaps as (
select
StartTime,
EndTime,
COUNT(*) as Cnt
from
Intervals i
inner join
#Sessions s
on
i.StartTime < s.SessionEnd and
s.SessionStart < i.EndTime
group by
StartTime,
EndTime
)
select * from IntervalOverlaps order by Cnt desc,StartTime
StartTime EndTime Cnt
---------------------- ---------------------- -----------
2012-01-03 00:00:00.00 2012-01-05 00:00:00.00 2
2012-01-07 00:00:00.00 2012-01-08 00:00:00.00 2
2012-01-01 00:00:00.00 2012-01-03 00:00:00.00 1
2012-01-05 00:00:00.00 2012-01-07 00:00:00.00 1
2012-01-08 00:00:00.00 2012-01-09 00:00:00.00 1