一个业务比较复杂

Rubi 2006-01-19 10:25:55
时间 退服时长(小时) 次数
08:00-22:00 4-8 1
8-12 2
12-14 3
22:00-08:00 >=8 1
时间用time 表示,我怎么样统计一天的退服次数呢?
因为数据库里面就是用time表示时间,一天24个小时的时间,如果退服的话,那么就有时间中断。
比如:
cell time
a 2006-1-19 00:00:00
a 2006-1-19 01:00:00--退服开始时间
a 2006-1-19 06:00:00--退服结束时间
a 2006-1-19 07:00:00
a 2006-1-19 08:00:00--退服开始时间
a 2006-1-19 22:00:00--退服结束时间
a 2006-1-19 23:00:00
我想大家在这里能否提供点思路


...全文
267 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rubi 2006-02-03
  • 打赏
  • 举报
回复
开年了,自己还没有想出拉力,顶一下
Rubi 2006-01-21
  • 打赏
  • 举报
回复
时间 退服时长(小时)次数
08:00-22:00 4-8 1
8-12 2
12-14 3
22:00-08:00 >=8 1

这个逻辑是怎么样的?因为退服次数还要按照这个逻辑来,那怎么去吧这个时间找出来,然后酸楚退服次数阿
Ninjai_Chan 2006-01-21
  • 打赏
  • 举报
回复
create table #1 (cell varchar(10),time datetime)

insert into #1(cell,time) select
'a' , '2006-1-19 00:00:00'
union all select
'a' , '2006-1-19 01:00:00'--退服开始时间
union all select
'a' , '2006-1-19 06:00:00'--退服结束时间
union all select
'a' , '2006-1-19 07:00:00'
union all select
'a' , '2006-1-19 08:00:00'--退服开始时间
union all select
'a' , '2006-1-19 22:00:00'--退服结束时间
union all select
'a' , '2006-1-19 23:00:00'
union all select
'a' , '2006-1-20 00:00:00'



select IDENTITY(int) AS ID,A.cell AS CELL,A.[time] AS TIME into #2 from #1 a
select * from
(SELECT A.CELL,DATENAME (HH,B.TIME-A.TIME) AS HH FROM #2 AS A,#2 AS B WHERE A.ID+1=B.ID
and a.time>='2006-1-19' and b.time<='2006-1-20')C
WHERE C.HH>1
--然后在分组取4-8,8-12,12-14 等分段数据
drop table #1
drop table #2
Ninjai_Chan 2006-01-21
  • 打赏
  • 举报
回复
ReViSion(和尚) ( )
按你的方法,请问当"a 2006-1-19 23:00:00"也处于退服状态呢?
rouqu 2006-01-21
  • 打赏
  • 举报
回复
明白了!
Rubi 2006-01-20
  • 打赏
  • 举报
回复
就是说如果一天24小时都有数据,比如每个小时的时间都显示出来,那就表示没有退服,如果之间有时间间隔,就表示有退服了
a 2006-1-19 01:00:00--退服开始时间
a 2006-1-19 06:00:00--退服结束时间

这里不连续,就表示从1点开始退服,然后6点结束了退服时间,如果都是联系的时间,比如
a 2006-1-19 01:00:00--退服开始时间
a 2006-1-19 02:00:00--退服结束时间
a 2006-1-19 03:00:00--退服开始时间
a 2006-1-19 04:00:00--退服结束时间
...全部连续,间隔为一个小时
a 2006-1-19 23:00:00--退服开始时间
就表示19号这天没有退服
ReViSion 2006-01-20
  • 打赏
  • 举报
回复
如果统计为按日期,cell汇总统计,可以用这个
create table #1 (cell varchar(10),time datetime)

insert into #1(cell,time) select
'a' , '2006-1-19 00:00:00'
union all select
'a' , '2006-1-19 01:00:00'--退服开始时间
union all select
'a' , '2006-1-19 06:00:00'--退服结束时间
union all select
'a' , '2006-1-19 07:00:00'
union all select
'a' , '2006-1-19 08:00:00'--退服开始时间
union all select
'a' , '2006-1-19 22:00:00'--退服结束时间
union all select
'a' , '2006-1-19 23:00:00'


--注意这里2006-1-19 00:00:00会被计算一次,所以多一次
select time,times=count(cell) from
(select cell,time=convert(varchar(10),time,120) from #1 a
where not exists(select 1 from #1 where cell=a.cell and [time]=dateadd(hh,-1,a.time)))a
group by a.time,cell
ReViSion 2006-01-20
  • 打赏
  • 举报
回复
或者这样,如果只是找一天的话,

create table #1 (cell varchar(10),time datetime)

insert into #1(cell,time) select
'a' , '2006-1-19 00:00:00'
union all select
'a' , '2006-1-19 01:00:00'--退服开始时间
union all select
'a' , '2006-1-19 06:00:00'--退服结束时间
union all select
'a' , '2006-1-19 07:00:00'
union all select
'a' , '2006-1-19 08:00:00'--退服开始时间
union all select
'a' , '2006-1-19 22:00:00'--退服结束时间
union all select
'a' , '2006-1-19 23:00:00'


Declare @times int
Declare @TempDate datetime
--这里比如要查找2006-1-19号
set @times=0

select @tempdate=[time],@times=case when @tempdate<>dateadd(hh,1,@tempdate1) then @times+1 else @times end,
@tempdate1=@tempdate from #1
where time between '2006-1-19' and '2006-1-19 23:59'
select @times

drop table #1
ReViSion 2006-01-20
  • 打赏
  • 举报
回复

create table #1 (cell varchar(10),time datetime)

insert into #1(cell,time) select
'a' , '2006-1-19 00:00:00'
union all select
'a' , '2006-1-19 01:00:00'--退服开始时间
union all select
'a' , '2006-1-19 06:00:00'--退服结束时间
union all select
'a' , '2006-1-19 07:00:00'
union all select
'a' , '2006-1-19 08:00:00'--退服开始时间
union all select
'a' , '2006-1-19 22:00:00'--退服结束时间
union all select
'a' , '2006-1-19 23:00:00'


Declare @times int
Declare @TempDate datetime
Declare @TempDate1 datetime
--这里比如要查找2006-1-19号
set @times=0

update #1 set @tempdate=time,@times=case when @tempdate<>dateadd(hh,1,@tempdate1) then @times+1 else @times end,
@tempdate1=@tempdate
where time between '2006-1-19' and '2006-1-19 23:59'
select @times

select * from #1
Rubi 2006-01-20
  • 打赏
  • 举报
回复
自己up
zheninchangjiang 2006-01-20
  • 打赏
  • 举报
回复
怎么说都不明白,所谓的退服是啥意思?发生在什么情况下的?
Rubi 2006-01-20
  • 打赏
  • 举报
回复
业务是这样的,数据库里面有两个字段
,一个是cell,一个是time,数据库里面有现成的数据,每天都有24个小时的数据记录,如果有哪个小时没有,就不会存入数据库,比如19号那天,数据库就有这些数据,那么19号的退服时间如下
那么如果小时之间不联系,就表示有退服,退服就表示有机器在那个时间段内没有工作了,比如19号1点后没有跟2点,那么就表示有机器推出服务了,那么就是退服开始了,然后再6点又有数据了,表示机器又开始工作了,那么1点到6点就是退服的时间,然后根据上面的公司来开始计算,不知道这样说明白否?
cell time
a 2006-1-19 00:00:00
a 2006-1-19 01:00:00--退服开始时间
a 2006-1-19 06:00:00--退服结束时间
a 2006-1-19 07:00:00
a 2006-1-19 08:00:00--退服开始时间
a 2006-1-19 22:00:00--退服结束时间
a 2006-1-19 23:00:00
rouqu 2006-01-20
  • 打赏
  • 举报
回复
其实这是个很简单的问题啊 把表设计好select...count..where不就好了?
rouqu 2006-01-20
  • 打赏
  • 举报
回复
还是要把处理逻辑说清楚 即我晚上23点退出游戏服务器 当日不再进来 这个算不算一次?
rouqu 2006-01-20
  • 打赏
  • 举报
回复
cell time
a 2006-1-19 00:00:00
a 2006-1-19 01:00:00--退服开始时间
a 2006-1-19 06:00:00--退服结束时间
a 2006-1-19 07:00:00
a 2006-1-19 08:00:00--退服开始时间
a 2006-1-19 22:00:00--退服结束时间
a 2006-1-19 23:00:00

我怎么样统计一天的退服次数呢?
------------------------------
你需要加退服标记列吧 要不怎么统计呢 如果你所指的退服次数需要算上当天下次登服的时间 直接求和有点问题
lvchy 2006-01-20
  • 打赏
  • 举报
回复
你的表结构中不是有“次数”的字段了吗?对这个字段进行统计就可以了,条件时间可以是一天之间
kkk9127 2006-01-20
  • 打赏
  • 举报
回复
还是不明白呀
aohan 2006-01-19
  • 打赏
  • 举报
回复
没看明白
Rubi 2006-01-19
  • 打赏
  • 举报
回复
因为如果不退服,那么就有24个小时的显示,如果中间断了,那么就是我这里写的
a 2006-1-19 01:00:00--退服开始时间
a 2006-1-19 06:00:00--退服结束时间
ReViSion 2006-01-19
  • 打赏
  • 举报
回复
中间还锱一个,什么意思呀
加载更多回复(1)

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧