22,222
社区成员
发帖
与我相关
我的任务
分享
(select
a.BeginTime,min(b.EndTime) as EndTime
from
(select distinct BeginTime from @tab union all select distinct EndTime from @tab) a,
(select distinct EndTime from @tab union all select distinct BeginTime from @tab) b
where
a.BeginTime<b.EndTime
group by
a.BeginTime) m
--创建测试数据
declare @tab table(Id int,BeginTime datetime,EndTime datetime)
insert into @tab select 1,'2003-04-02 10:03:04','2003-4-02 13:03:04'
insert into @tab select 2,'2003-04-03 02:03:04','2003-4-03 03:03:04'
insert into @tab select 3,'2003-03-02 11:03:04','2003-4-02 13:03:04'
insert into @tab select 4,'2003-04-02 12:03:04','2003-4-02 13:03:04'
insert into @tab select 5,'2003-04-02 13:03:04','2003-4-02 15:03:04'
insert into @tab select 6,'2003-04-02 14:03:04','2009-4-02 17:03:04'
--建一个中间表,保存所有时段使用人数的统计数据
declare @rec table(BeginTime datetime,EndTime datetime,num int)
--生成统计数据并插入中间表
insert into @rec
select
m.BeginTime,m.EndTime,count(n.id) as num
from
(select
a.BeginTime,min(b.EndTime) as EndTime
from
(select distinct BeginTime from @tab union all select distinct EndTime from @tab) a,
(select distinct EndTime from @tab union all select distinct BeginTime from @tab) b
where
a.BeginTime<b.EndTime
group by
a.BeginTime) m left join @tab n
on
(m.BeginTime between n.BeginTime and n.EndTime)
and
(m.EndTime between n.BeginTime and n.EndTime)
group by
m.BeginTime,m.EndTime
having count(n.id)>0
--查询出所有使用人数最少的时段
select t.* from @rec t where not exists(select 1 from @rec where num<t.num)
/*
BeginTime EndTime num
------------------------------------------------------ ------------------------------------------------------ -----------
2003-03-02 11:03:04.000 2003-04-02 10:03:04.000 1
2003-04-02 13:03:04.000 2003-04-02 14:03:04.000 1
2003-04-02 15:03:04.000 2003-04-03 02:03:04.000 1
2003-04-03 03:03:04.000 2009-04-02 17:03:04.000 1
*/
--查询出所有使用人数最多的时段
select t.* from @rec t where not exists(select 1 from @rec where num>t.num)
/*
BeginTime EndTime num
------------------------------------------------------ ------------------------------------------------------ -----------
2003-04-02 12:03:04.000 2003-04-02 13:03:04.000 3
*/
--最大
select top 1 max(1) from @tab group by EndTime-BeginTime --按照你的时间段来分组吧
order by max(1) desc
--最小
select top 1 min(1) from @tab group by EndTime-BeginTime --按照你的时间段来分组吧
order by min(1) desc
declare @tab table(Id int,BeginTime datetime,EndTime datetime)
insert into @tab select 1,'2003-04-02 10:03:04','2003-4-02 13:03:04'
insert into @tab select 2,'2003-04-03 02:03:04','2003-4-03 03:03:04'
insert into @tab select 3,'2003-03-02 11:03:04','2003-4-02 13:03:04'
insert into @tab select 4,'2003-04-02 12:03:04','2003-4-02 13:03:04'
insert into @tab select 5,'2003-04-02 13:03:04','2003-4-02 15:03:04'
insert into @tab select 6,'2003-04-02 14:03:04','2009-4-02 17:03:04'
select
m.BeginTime,m.EndTime,count(n.id) as num
from
(select
a.BeginTime,min(b.EndTime) as EndTime
from
(select distinct BeginTime from @tab union all select distinct EndTime from @tab) a,
(select distinct EndTime from @tab union all select distinct BeginTime from @tab) b
where
a.BeginTime<b.EndTime
group by
a.BeginTime) m left join @tab n
on
(m.BeginTime between n.BeginTime and n.EndTime)
and
(m.EndTime between n.BeginTime and n.EndTime)
group by
m.BeginTime,m.EndTime
order by
m.BeginTime,m.EndTime
/*
BeginTime EndTime num
------------------------------------------------------ ------------------------------------------------------ -----------
2003-03-02 11:03:04.000 2003-04-02 10:03:04.000 1
2003-04-02 10:03:04.000 2003-04-02 12:03:04.000 2
2003-04-02 12:03:04.000 2003-04-02 13:03:04.000 3
2003-04-02 13:03:04.000 2003-04-02 14:03:04.000 1
2003-04-02 14:03:04.000 2003-04-02 15:03:04.000 2
2003-04-02 15:03:04.000 2003-04-03 02:03:04.000 1
2003-04-03 02:03:04.000 2003-04-03 03:03:04.000 2
2003-04-03 03:03:04.000 2009-04-02 17:03:04.000 1
*/
declare @tab table(Id int,BeginTime datetime,EndTime datetime)
insert into @tab select 1,'2003-04-02 10:03:04','2003-4-02 13:03:04'
insert into @tab select 2,'2003-04-03 02:03:04','2003-4-03 03:03:04'
insert into @tab select 3,'2003-03-02 11:03:04','2003-4-02 13:03:04'
insert into @tab select 4,'2003-04-02 12:03:04','2003-4-02 13:03:04'
insert into @tab select 5,'2003-04-02 13:03:04','2003-4-02 15:03:04'
insert into @tab select 6,'2003-04-02 14:03:04','2009-4-02 17:03:04'
select
m.BeginTime,m.EndTime,count(n.id) as num
from
(select
a.BeginTime,min(b.EndTime) as EndTime
from
(select distinct BeginTime from @tab union all select distinct EndTime from @tab) a,
(select distinct EndTime from @tab union all select distinct BeginTime from @tab) b
where
a.BeginTime<b.EndTime
group by
a.BeginTime) m left join @tab n
on
(m.BeginTime between n.BeginTime and n.EndTime)
and
(m.EndTime between n.BeginTime and n.EndTime)
group by
m.BeginTime,m.EndTime
order by
num desc
/*
BeginTime EndTime num
------------------------------------------------------ ------------------------------------------------------ -----------
2003-04-02 12:03:04.000 2003-04-02 13:03:04.000 3
2003-04-02 10:03:04.000 2003-04-02 12:03:04.000 2
2003-04-02 14:03:04.000 2003-04-02 15:03:04.000 2
2003-04-03 02:03:04.000 2003-04-03 03:03:04.000 2
2003-04-03 03:03:04.000 2009-04-02 17:03:04.000 1
2003-04-02 15:03:04.000 2003-04-03 02:03:04.000 1
2003-03-02 11:03:04.000 2003-04-02 10:03:04.000 1
2003-04-02 13:03:04.000 2003-04-02 14:03:04.000 1
*/