SQL语句的收集
1.对每个时间段的汇总和求比例!
Code
------------------------------------
--用途:对文件到报的时序统计(每个台站,每个文件类型,每个时间段,文件的到报率, 迟报率,未报率)
--说明:
------------------------------------
ALTER PROCEDURE UP_OldMessageSequence_GetSequence
@StationId char(5) , --台站名
@MessageType varchar(20) , --文件类型
@Btime datetime , --查询开始的时间
@Etime datetime --查询结束的时间
AS
if @stationId ='' and @MessageType =''
begin
select convert(varchar(10),MessageStandardTime,120) as '时间段' ,left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5)+'%' as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5)+'%' as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5)+'%' as '未报率' from OldMessageSequence
where MessageStandardTime between convert(varchar(10),@Btime,120) and convert(varchar(10),@Etime,120) group by convert(varchar(10),MessageStandardTime,120)
end
if @stationId !='' and @MessageType =''
begin
select stationId as '台站号', convert(varchar(10),MessageStandardTime,120) as '时间段',left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5)+'%'as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5)+'%'as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5)+'%'as '未报率' from OldMessageSequence
where MessageStandardTime between convert(varchar(10),@Btime,120) and convert(varchar(10),@Etime,120) and stationId = @StationId group by stationId ,convert(varchar(10),MessageStandardTime,120)
end
if @stationId =''and @MessageType !=''
begin
select MessageType as '文件类型' , convert(varchar(10),MessageStandardTime,120) as '时间段',left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5)+'%' as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5)+'%'as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5)+'%' as '未报率' from OldMessageSequence
where MessageStandardTime between convert(varchar(10),@Btime,120) and convert(varchar(10),@Etime,120) and MessageType = @MessageType group by MessageType ,convert(varchar(10),MessageStandardTime,120)
end
if @stationId !=''and @MessageType !=''
begin
select stationId as '台站号' ,MessageType as '文件类型', convert(varchar(10),MessageStandardTime,120) as '时间段',left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5)+'%' as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5)+'%' as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5)+'%' as '未报率'from OldMessageSequence
where MessageStandardTime between convert(varchar(10),@Btime,120) and convert(varchar(10),@Etime,120) and stationId = @StationId and MessageType = @MessageType group by stationId,MessageType ,convert(varchar(10),MessageStandardTime,120)
end
------------------------------------
--用途:对文件到报的时序统计(每个台站,每个文件类型,每个时间段,文件的到报率, 迟报率,未报率)
--说明:
------------------------------------
ALTER PROCEDURE UP_OldMessageSequence_GetSequence
@StationId char(5) , --台站名
@MessageType varchar(20) , --文件类型
@Btime datetime , --查询开始的时间
@Etime datetime --查询结束的时间
AS
if @stationId ='' and @MessageType =''
begin
select convert(varchar(10),MessageStandardTime,120) as '时间段' ,left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5)+'%' as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5)+'%' as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5)+'%' as '未报率' from OldMessageSequence
where MessageStandardTime between convert(varchar(10),@Btime,120) and convert(varchar(10),@Etime,120) group by convert(varchar(10),MessageStandardTime,120)
end
if @stationId !='' and @MessageType =''
begin
select stationId as '台站号', convert(varchar(10),MessageStandardTime,120) as '时间段',left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5)+'%'as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5)+'%'as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5)+'%'as '未报率' from OldMessageSequence
where MessageStandardTime between convert(varchar(10),@Btime,120) and convert(varchar(10),@Etime,120) and stationId = @StationId group by stationId ,convert(varchar(10),MessageStandardTime,120)
end
if @stationId =''and @MessageType !=''
begin
select MessageType as '文件类型' , convert(varchar(10),MessageStandardTime,120) as '时间段',left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5)+'%' as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5)+'%'as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5)+'%' as '未报率' from OldMessageSequence
where MessageStandardTime between convert(varchar(10),@Btime,120) and convert(varchar(10),@Etime,120) and MessageType = @MessageType group by MessageType ,convert(varchar(10),MessageStandardTime,120)
end
if @stationId !=''and @MessageType !=''
begin
select stationId as '台站号' ,MessageType as '文件类型', convert(varchar(10),MessageStandardTime,120) as '时间段',left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5)+'%' as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5)+'%' as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5)+'%' as '未报率'from OldMessageSequence
where MessageStandardTime between convert(varchar(10),@Btime,120) and convert(varchar(10),@Etime,120) and stationId = @StationId and MessageType = @MessageType group by stationId,MessageType ,convert(varchar(10),MessageStandardTime,120)
end
2.对多表中的数据总计查询!
Code
ALTER PROCEDURE dbo.radarstate1
@StationId char(5) , --台站号(为空表示为查询全部台站的信息)
@MessageType varchar(14) , --报文类型
@BeginTime datetime , --查询开始时间
@EndTime datetime --查询结束时间
AS
if @StationId = ''
begin
select b.StationId as '台站号',left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5) as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5) as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5) as '缺报率', (select count(*) from ServiceAlarm where StationId = b.StationId and MessageType = @MessageType and MessageStandardTime between convert(varchar(10),@BeginTime,120) and convert(varchar(10),@EndTime,120)) as '报警次数'
from OldMessageSequence as b where b.MessageStandardTime between convert(varchar(10),@BeginTime,120) and convert(varchar(10),@EndTime,120) and b.MessageType = @MessageType group by b.StationId
end
if @StationId !=''
begin
select convert(varchar(10),MessageStandardTime,120) as '时间段',left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5) as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5) as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5) as '缺报率', (select count(*) from ServiceAlarm where StationId = @StationId and MessageType = @MessageType and MessageStandardTime between convert(varchar(10),@BeginTime,120) and convert(varchar(10),@EndTime,120)) as '报警次数'
from OldMessageSequence as b where b.MessageStandardTime between convert(varchar(10),@BeginTime,120) and convert(varchar(10),@EndTime,120) and b.MessageType = @MessageType and b.StationId=@StationId group by convert(varchar(10),MessageStandardTime,120)
end
RETURN
ALTER PROCEDURE dbo.radarstate1
@StationId char(5) , --台站号(为空表示为查询全部台站的信息)
@MessageType varchar(14) , --报文类型
@BeginTime datetime , --查询开始时间
@EndTime datetime --查询结束时间
AS
if @StationId = ''
begin
select b.StationId as '台站号',left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5) as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5) as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5) as '缺报率', (select count(*) from ServiceAlarm where StationId = b.StationId and MessageType = @MessageType and MessageStandardTime between convert(varchar(10),@BeginTime,120) and convert(varchar(10),@EndTime,120)) as '报警次数'
from OldMessageSequence as b where b.MessageStandardTime between convert(varchar(10),@BeginTime,120) and convert(varchar(10),@EndTime,120) and b.MessageType = @MessageType group by b.StationId
end
if @StationId !=''
begin
select convert(varchar(10),MessageStandardTime,120) as '时间段',left(ltrim(sum(case when TimeFlag=1 or TimeFlag =2 then 1 else 0 end )*100.0/count(*)),5) as '到报率' ,left(ltrim(sum(case when TimeFlag=2 then 1 else 0 end )*100.0/count(*)),5) as '迟报率',left(ltrim(sum(case when TimeFlag =3 then 1 else 0 end )*100.0/count(*)),5) as '缺报率', (select count(*) from ServiceAlarm where StationId = @StationId and MessageType = @MessageType and MessageStandardTime between convert(varchar(10),@BeginTime,120) and convert(varchar(10),@EndTime,120)) as '报警次数'
from OldMessageSequence as b where b.MessageStandardTime between convert(varchar(10),@BeginTime,120) and convert(varchar(10),@EndTime,120) and b.MessageType = @MessageType and b.StationId=@StationId group by convert(varchar(10),MessageStandardTime,120)
end
RETURN