有如下的一个表
Createtime type onlineNumber
2008-11-5 19:30:21 大厅1 22
2008-11-5 19:30:21 大厅2 232
2008-11-5 19:30:22 大厅3 78
2008-11-5 19:45:22 大厅1 14
2008-11-5 19:45:22 大厅2 35
2008-11-5 19:45:22 大厅3 67
. . .
. . .
. . .
2008-11-6 00:15:00 大厅1 23
. . .
. . .
. . .这个表是 每隔15分中会往里面插三条数据的 onlinenumber 是在线人数 三个大厅相加是这个时间的 总在线人数现在要求的sql语句是 :选出每天最高的onlinenumber 和,比如2008-11-5 是 在19:30 这个时间 里 大厅1+大厅2+大厅3 的三个 类型的number 的和,也就是这个时间的总在线人数, 是最高(大厅1的22+大厅2的232+大厅3的78=332 )
11-5 这一天 最高是19:30 的 总在线人数,为332
11-6 这一天 最高是XX:XX 这个时间 的总在线人数最高 为XXX
11-7.......
以此类推 如何在给出 Starttime 和endtime 的情况下 select出来每天最高在线人数,按日期升序排列
Createtime type onlineNumber
2008-11-5 19:30:21 大厅1 22
2008-11-5 19:30:21 大厅2 232
2008-11-5 19:30:22 大厅3 78
2008-11-5 19:45:22 大厅1 14
2008-11-5 19:45:22 大厅2 35
2008-11-5 19:45:22 大厅3 67
. . .
. . .
. . .
2008-11-6 00:15:00 大厅1 23
. . .
. . .
. . .这个表是 每隔15分中会往里面插三条数据的 onlinenumber 是在线人数 三个大厅相加是这个时间的 总在线人数现在要求的sql语句是 :选出每天最高的onlinenumber 和,比如2008-11-5 是 在19:30 这个时间 里 大厅1+大厅2+大厅3 的三个 类型的number 的和,也就是这个时间的总在线人数, 是最高(大厅1的22+大厅2的232+大厅3的78=332 )
11-5 这一天 最高是19:30 的 总在线人数,为332
11-6 这一天 最高是XX:XX 这个时间 的总在线人数最高 为XXX
11-7.......
以此类推 如何在给出 Starttime 和endtime 的情况下 select出来每天最高在线人数,按日期升序排列
from tb
group by createtime
from table
group by substring(cast(Createtime as varchar(18),1,15)
order by Createtime
create table y_table(Createtime nvarchar(30),type varchar(5),onlineNumber int)
insert into y_table
select '2008-11-5 19:30:21','大厅1',22 union all
select '2008-11-5 19:30:21','大厅2',232 union all
select '2008-11-5 19:30:22','大厅3',78 union all
select '2008-11-5 19:45:22','大厅1',14 union all
select '2008-11-5 19:45:22','大厅2',35 union all
select '2008-11-5 19:45:22','大厅3',67 union all
select '2008-11-6 19:30:21','大厅1',12 union all
select '2008-11-6 19:30:21','大厅2',43 union all
select '2008-11-6 19:30:22','大厅3',12 union all
select '2008-11-6 19:45:22','大厅1',45 union all
select '2008-11-6 19:45:22','大厅2',22 union all
select '2008-11-6 19:45:22','大厅3',17select * from y_table
create function [dbo].[m_yy]
(
@centers varchar(2000)
)
returns int
as
begin
declare @ff int
select @ff=sum(onlineNumber) from y_table where Createtime like @centers+'%'
return @ff
end
go
create function [dbo].[m_zz]
(
@centers varchar(2000)
)
returns int
as
begin
declare @ff int
select @ff=max(onlineNumber) from y_table where Createtime like @centers+'%'
return @ff
end
select distinct
substring(dd.bb,1,9) as 日期,
dbo.m_zz(substring(dd.bb,1,9)) as 最高值
from
(
select cc.bb,dbo.m_yy(cc.bb) as maxval from (select substring(Createtime,1,15) as bb
from y_table group by substring(Createtime,1,15)
)
cc
)
dd/*结果
日期 最高值
--------- -----------
2008-11-5 232
2008-11-6 45
*/
select cc.bb,dbo.m_yy(cc.bb) as maxval from (select substring(Createtime,1,15) as bb
from y_table group by substring(Createtime,1,15)
)
cc
/*
bb maxval
--------------- -----------
2008-11-5 19:30 332
2008-11-5 19:45 116
2008-11-6 19:30 67
2008-11-6 19:45 84
*/
create table y_table(Createtime nvarchar(30),type varchar(5),onlineNumber int)
--测试数据
insert into y_table
select '2008-11-5 19:30:21','大厅1',22 union all
select '2008-11-5 19:30:21','大厅2',232 union all
select '2008-11-5 19:30:22','大厅3',78 union all
select '2008-11-5 19:45:22','大厅1',14 union all
select '2008-11-5 19:45:22','大厅2',35 union all
select '2008-11-5 19:45:22','大厅3',67 union all
select '2008-11-6 19:30:21','大厅1',12 union all
select '2008-11-6 19:30:21','大厅2',43 union all
select '2008-11-6 19:30:22','大厅3',12 union all
select '2008-11-6 19:45:22','大厅1',45 union all
select '2008-11-6 19:45:22','大厅2',22 union all
select '2008-11-6 19:45:22','大厅3',17
--查看
select * from y_table
/*
Createtime type onlineNumber
------------------------------ ----- ------------
2008-11-5 19:30:21 大厅1 22
2008-11-5 19:30:21 大厅2 232
2008-11-5 19:30:22 大厅3 78
2008-11-5 19:45:22 大厅1 14
2008-11-5 19:45:22 大厅2 35
2008-11-5 19:45:22 大厅3 67
2008-11-6 19:30:21 大厅1 12
2008-11-6 19:30:21 大厅2 43
2008-11-6 19:30:22 大厅3 12
2008-11-6 19:45:22 大厅1 45
2008-11-6 19:45:22 大厅2 22
2008-11-6 19:45:22 大厅3 17
*/
go
--创建函数
create function [dbo].[m_yy]
(
@centers varchar(2000)
)
returns int
as
begin
declare @ff int
select @ff=sum(onlineNumber) from y_table where Createtime like @centers+'%'
return @ff
end
go
--创建函数
create function [dbo].[m_zz]
(
@centers varchar(2000)
)
returns int
as
begin
declare @ff int
select @ff=max(dd.maxval) from
(
select cc.bb,dbo.m_yy(cc.bb) as maxval from
(
select substring(Createtime,1,15) as bb
from y_table group by substring(Createtime,1,15)
)
cc)dd
where bb like @centers+'%'
return @ff
end
go
--所要结果
select
distinct substring(bb,1,9) as 日期,
dbo.m_zz(substring(bb,1,9)) as 最大值
from
(
select cc.bb,dbo.m_yy(cc.bb) as maxval from
(
select substring(Createtime,1,15) as bb from y_table group by substring(Createtime,1,15)
)cc
)dd/*查看结果
日期 最大值
--------- -----------
2008-11-5 332
2008-11-6 84
*/
go
--删除表及函数
drop function m_yy
drop function m_zz
drop table y_table