做报表时用的存储和计划任务,留个备份。

由于查询的数据量很大,所以写了好几个:

USE [SGPS_SZ]
GO
/****** Object:  StoredProcedure [dbo].[spMileageStatement]    Script Date: 07/19/2012 13:39:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        <Author,,YangSongKai>
-- Create date: <Create 2012-07-02,,>
-- Description:    <Description,,里程报表>
-- =============================================

ALTER PROCEDURE [dbo].[spMileageStatement](
 @CarNo varchar(30),--车牌号
 @CompanyId varchar(100),--驾校id
 @StartDay varchar(20),--开始日期(年月日)
 @EndDay varchar(20),--结束日期
 @StartTime varchar(20),--开始时间(时分秒)
 @EndTime varchar(20)--结束时间
    )
AS

--自定义变量
declare @result int--日期相差多少天
declare @S datetime--开始时间
declare @E datetime--结束时间
declare @GpsPath varchar(30)--表名
declare @number int--循环种子
declare @WHERES VARCHAR(1000)
declare @SQL VARCHAR(3000)
declare @t varchar(20)
set @S=CONVERT(Datetime,@StartDay+@StartTime)
set @E=CONVERT(Datetime,@StartDay+@EndTime)
--set @GpsPath='dbo.GpsPath'+CONVERT( VARCHAR(8), CAST(FLOOR(CAST(@S AS FLOAT)) AS DATETIME), 112)
set @number=0
set @result=DATEDIFF (d,CONVERT(Datetime,@StartDay),CONVERT(Datetime,@EndDay))

create TABLE #temptb    
(   
    TerminalNo varchar(20),
    CarNo varchar(20),
    CompanyName varchar(50),
    Mileage decimal(18,2)
)
 
declare @begin_date datetime declare @end_date datetime select @begin_date = getdate() 
    while(@number<=@result)
    begin
    set @GpsPath='dbo.GpsPath'+CONVERT( VARCHAR(8), CAST(FLOOR(CAST(@S AS FLOAT)) AS DATETIME), 112)
    IF(@CarNo='' or @CarNo is null)
    SET @WHERES=' AND dbo.Company.CompanyId='''+@CompanyId+''' and GpsTime between '''+CONVERT(VARCHAR(19),@S,120)+''' and '''+CONVERT(VARCHAR(19),@E,120)+''''
    --ELSE
    --begin
    --set @t=(select TerminalNo from Terminal where CarId=(select CarId from Car where CarNo=@CarNo))
   -- set @WHERES=' and GpsTime between '''+CONVERT(VARCHAR(19),@S,120)+''' and '''+CONVERT(VARCHAR(19),@E,120)+''' and dbo.Terminal.TerminalNo='''+@t+''''
    --end
    --循环查出数据集插入到临时表中
    SET @SQL='insert into #temptb(TerminalNo,CarNo,CompanyName,Mileage)(SELECT TerminalNo,CarNo,CompanyName,
(SELECT  top 1 Mileage 
FROM  dbo.Car 
INNER JOIN dbo.Terminal ON dbo.Car.CarId = dbo.Terminal.CarId 
INNER JOIN '+@GpsPath+' ON dbo.Terminal.TerminalNo = '+@GpsPath+'.TerminalNo
WHERE GpsTime<='''+CONVERT(VARCHAR(19),@E,120)+''' AND Terminal.TerminalNo=T.TerminalNo AND Mileage>0
ORDER BY GpsTime desc )-(SELECT  top 1 Mileage
FROM  dbo.Car 
INNER JOIN dbo.Terminal ON dbo.Car.CarId = dbo.Terminal.CarId 
INNER JOIN '+@GpsPath+' ON dbo.Terminal.TerminalNo = '+@GpsPath+'.TerminalNo AND Mileage>0
WHERE GpsTime>='''+CONVERT(VARCHAR(19),@S,120)+''' AND Terminal.TerminalNo=T.TerminalNo
ORDER BY GpsTime asc)AS    Mileage
FROM
(SELECT '+@GpsPath+'.TerminalNo, MIN(GpsTime)AS StartTime,MAX(GpsTime)AS EndTime,CarNo,CompanyName
from '+@GpsPath+'
inner join dbo.Terminal on '+@GpsPath+'.TerminalNo=dbo.Terminal.TerminalNo
inner join dbo.Car on dbo.Terminal.CarId=dbo.Car.CarId
inner join dbo.Company on dbo.Car.CompanyId=dbo.Company.CompanyId 
 WHERE 1=1 '+@WHERES+'
group by '+@GpsPath+'.TerminalNo,CarNo,CompanyName)T)'
    Exec(@SQL) 
    set @number=@number+1
    set @S=DATEADD(d,1,@S)
    set @E=DATEADD(d,1,@E)
    end
select ROW_NUMBER() Over(order by CompanyName desc) as rowId, TerminalNo,CarNo,CompanyName,SUM(Mileage)as Mileage from #temptb group by TerminalNo,CarNo,CompanyName 
if object_id('tempdb..#emptb')>0
drop table #temptb
select @end_date = getdate() select datediff(ms,@begin_date,@end_date) as '用时/毫秒'

2.

USE [SGPS_SZ]
GO
/****** Object:  StoredProcedure [dbo].[spMileageStatement]    Script Date: 07/19/2012 13:39:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        <Author,,YangSongKai>
-- Create date: <Create 2012-07-02,,>
-- Description:    <Description,,里程报表>
-- =============================================

ALTER PROCEDURE [dbo].[spMileageStatement](
 @CarNo varchar(30),--车牌号
 @CompanyId varchar(100),--驾校id
 @StartDay varchar(20),--开始日期(年月日)
 @EndDay varchar(20),--结束日期
 @StartTime varchar(20),--开始时间(时分秒)
 @EndTime varchar(20)--结束时间
    )
AS

--自定义变量
declare @result int--日期相差多少天
declare @S datetime--开始时间
declare @E datetime--结束时间
declare @GpsPath varchar(30)--表名
declare @number int--循环种子
declare @WHERES VARCHAR(1000)
declare @SQL VARCHAR(3000)
declare @t varchar(20)
set @S=CONVERT(Datetime,@StartDay+@StartTime)
set @E=CONVERT(Datetime,@StartDay+@EndTime)
--set @GpsPath='dbo.GpsPath'+CONVERT( VARCHAR(8), CAST(FLOOR(CAST(@S AS FLOAT)) AS DATETIME), 112)
set @number=0
set @result=DATEDIFF (d,CONVERT(Datetime,@StartDay),CONVERT(Datetime,@EndDay))

create TABLE #temptb    
(   
    TerminalNo varchar(20),
    CarNo varchar(20),
    Gpstime varchar(100),
    CompanyName varchar(50),
    Mileage decimal(18,2),
)

 
declare @begin_date datetime declare @end_date datetime select @begin_date = getdate() 
    while(@number<=@result)
    begin
    set @GpsPath='dbo.GpsPath'+CONVERT( VARCHAR(8), CAST(FLOOR(CAST(@S AS FLOAT)) AS DATETIME), 112)
    IF(@CarNo='' or @CarNo is null)
    SET @WHERES=' AND dbo.Company.CompanyId='''+@CompanyId+''' and GpsTime between '''+CONVERT(VARCHAR(19),@S,120)+''' and '''+CONVERT(VARCHAR(19),@E,120)+''''
    ELSE
    begin
    set @t=(select TerminalNo from Terminal where CarId=(select CarId from Car where CarNo=@CarNo))
    set @WHERES=' and GpsTime between '''+CONVERT(VARCHAR(19),@S,120)+''' and '''+CONVERT(VARCHAR(19),@E,120)+''' and dbo.Terminal.TerminalNo='''+@t+''''
    end
    --循环查出数据集插入到临时表中
    SET @SQL='insert into #temptb(TerminalNo,CarNo,Gpstime,CompanyName,Mileage)(SELECT '+@GpsPath+'.TerminalNo,CarNo,GpsTime,CompanyName,Mileage 
    from '+@GpsPath+'
    inner join dbo.Terminal on '+@GpsPath+'.TerminalNo=dbo.Terminal.TerminalNo
    inner join dbo.Car on dbo.Terminal.CarId=dbo.Car.CarId
    inner join dbo.Company on dbo.Car.CompanyId=dbo.Company.CompanyId 
     WHERE 1=1 '+@WHERES+'
    group by '+@GpsPath+'.TerminalNo,CarNo,GpsTime,CompanyName,dbo.Company.CompanyId,Mileage)'
    Exec(@SQL) 
    
    set @number=@number+1
    set @S=DATEADD(d,1,@S)
    set @E=DATEADD(d,1,@E)
    end


    select ROW_NUMBER() Over(order by CompanyName desc) as rowId,TerminalNo,CarNo,CompanyName,sum(Mileage) Mileage
    from(
        select TerminalNo,CarNo,CompanyName,convert(varchar(8),Gpstime,112) date,
               max(Mileage) - min(Mileage) mileage
        from #temptb
        group by TerminalNo,CarNo,CompanyName,convert(varchar(8),Gpstime,112)
    ) t
    group by TerminalNo,CarNo,CompanyName --order by TerminalNo asc
        
if object_id('tempdb..#emptb')>0
drop table #temptb



select @end_date = getdate() select datediff(ms,@begin_date,@end_date) as '用时/毫秒'

3.

USE [SGPS_SZ]
GO
/****** Object:  StoredProcedure [dbo].[spMileageStatement]    Script Date: 07/20/2012 08:54:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        <Author,,YangSongKai>
-- Create date: <Create 2012-07-02,,>
-- Description:    <Description,,里程报表>
-- =============================================

ALTER PROCEDURE [dbo].[spMileageStatement](
 @CarNo varchar(30),--车牌号
 @CompanyId varchar(100),--驾校id
 @StartDay varchar(20),--开始日期(年月日)
 @EndDay varchar(20),--结束日期
 @StartTime varchar(20),--开始时间(时分秒)
 @EndTime varchar(20)--结束时间
    )
AS

--自定义变量
declare @result int--日期相差多少天
declare @S datetime--开始时间
declare @E datetime--结束时间
declare @GpsPath varchar(30)--表名
declare @number int--循环种子
declare @WHERES VARCHAR(1000)
declare @SQL VARCHAR(3000)
declare @t varchar(20)
set @S=CONVERT(Datetime,@StartDay+@StartTime)
set @E=CONVERT(Datetime,@StartDay+@EndTime)
--set @GpsPath='dbo.GpsPath'+CONVERT( VARCHAR(8), CAST(FLOOR(CAST(@S AS FLOAT)) AS DATETIME), 112)
set @number=0
set @result=DATEDIFF (d,CONVERT(Datetime,@StartDay),CONVERT(Datetime,@EndDay))

create TABLE #temptb    
(   
    TerminalNo varchar(20),
    CarNo varchar(20),
    Gpstime varchar(100),
    CompanyName varchar(50),
    Mileage decimal(18,2),
)

 
declare @begin_date datetime declare @end_date datetime select @begin_date = getdate() 
    while(@number<=@result)
    begin
    set @GpsPath='dbo.GpsPath'+CONVERT( VARCHAR(8), CAST(FLOOR(CAST(@S AS FLOAT)) AS DATETIME), 112)
    IF(@CarNo='' or @CarNo is null)
    --SET @WHERES=' AND dbo.Company.CompanyId='''+@CompanyId+''' and GpsTime between '''+CONVERT(VARCHAR(19),@S,120)+''' and '''+CONVERT(VARCHAR(19),@E,120)+''''
    SET @WHERES=' and GpsTime between '''+CONVERT(VARCHAR(19),@S,120)+''' and '''+CONVERT(VARCHAR(19),@E,120)+'''  and EXISTS (select TerminalNo from Terminal where EXISTS (select CarId from Car where CompanyId='''+@CompanyId+''' and CarId=Terminal.CarId) and TerminalNo=Terminal.TerminalNo)'
    ELSE
    begin
    set @t=(select TerminalNo from Terminal where CarId=(select CarId from Car where CarNo=@CarNo))
    set @WHERES=' and GpsTime between '''+CONVERT(VARCHAR(19),@S,120)+''' and '''+CONVERT(VARCHAR(19),@E,120)+''' and dbo.Terminal.TerminalNo='''+@t+''''
    end
    
    SET @SQL='insert into #temptb(TerminalNo,GpsTime,Mileage)(SELECT TerminalNo,GpsTime,Mileage 
    from '+@GpsPath+'
     WHERE 1=1 '+@WHERES+' 
    )'
    Exec(@SQL) 
    set @number=@number+1
    set @S=DATEADD(d,1,@S)
    set @E=DATEADD(d,1,@E)
    end
select t.TerminalNo,sum(Mileage) Mileage,CarNo,CompanyName
from(
    select TerminalNo,convert(varchar(8),Gpstime,112) date,
           max(Mileage) - min(Mileage) mileage
    from #temptb
    group by TerminalNo,convert(varchar(8),Gpstime,112)
) t
inner join dbo.Terminal on t.TerminalNo=dbo.Terminal.TerminalNo
    inner join dbo.Car on dbo.Terminal.CarId=dbo.Car.CarId
    inner join dbo.Company on dbo.Car.CompanyId=dbo.Company.CompanyId 
group by t.TerminalNo,CarNo,CompanyName
        
if object_id('tempdb..#emptb')>0
drop table #temptb



select @end_date = getdate() select datediff(ms,@begin_date,@end_date) as '用时/毫秒'

4.

USE [SGPS_SZ]
GO
/****** Object:  StoredProcedure [dbo].[spMileageStatement]    Script Date: 07/23/2012 09:54:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        <Author,,YangSongKai>
-- Create date: <Create 2012-07-02,,>
-- Description:    <Description,,里程报表>
-- =============================================

ALTER PROCEDURE [dbo].[spMileageStatement](
 @CarNo varchar(30),--车牌号
 @CompanyId varchar(100),--驾校id
 @StartDay varchar(20),--开始日期(年月日)
 @EndDay varchar(20),--结束日期
 @StartTime varchar(20),--开始时间(时分秒)
 @EndTime varchar(20)--结束时间
    )
AS

--自定义变量
declare @result int--日期相差多少天
declare @S datetime--开始时间
declare @E datetime--结束时间
declare @GpsPath varchar(30)--表名
declare @number int--循环种子
declare @WHERES VARCHAR(1000)
declare @SQL VARCHAR(3000)
declare @t varchar(20)
set @S=CONVERT(Datetime,@StartDay+@StartTime)
set @E=CONVERT(Datetime,@StartDay+@EndTime)
--set @GpsPath='dbo.GpsPath'+CONVERT( VARCHAR(8), CAST(FLOOR(CAST(@S AS FLOAT)) AS DATETIME), 112)
set @number=0
set @result=DATEDIFF (d,CONVERT(Datetime,@StartDay),CONVERT(Datetime,@EndDay))

create Table #temptb
(
    TerminalNo varchar(20),
    MinMileage decimal(18,2),
    MaxMileage decimal(18,2),
)
 
declare @begin_date datetime declare @end_date datetime select @begin_date = getdate() 
    while(@number<=@result)
    begin
    set @GpsPath='dbo.GpsPath'+CONVERT( VARCHAR(8), CAST(FLOOR(CAST(@S AS FLOAT)) AS DATETIME), 112)
    IF(@CarNo='' or @CarNo is null)
    --SET @WHERES=' AND dbo.Company.CompanyId='''+@CompanyId+''' and GpsTime between '''+CONVERT(VARCHAR(19),@S,120)+''' and '''+CONVERT(VARCHAR(19),@E,120)+''''
    --SET @WHERES=' and GpsTime between '''+CONVERT(VARCHAR(19),@S,120)+''' and '''+CONVERT(VARCHAR(19),@E,120)+''' and Mileage>0 and EXISTS (select TerminalNo from Terminal where EXISTS (select CarId from Car where CompanyId='''+@CompanyId+''' and CarId=Terminal.CarId) and '+@GpsPath+'.TerminalNo=Terminal.TerminalNo)'
    SET @WHERES='and GpsTime between '''+CONVERT(VARCHAR(19),@S,120)+''' and '''+CONVERT(VARCHAR(19),@E,120)+''' and Mileage>0'
    ELSE
    begin
    set @t=(select TerminalNo from Terminal where CarId=(select CarId from Car where CarNo=@CarNo))
    set @WHERES=' and GpsTime between '''+CONVERT(VARCHAR(19),@S,120)+''' and '''+CONVERT(VARCHAR(19),@E,120)+''' and TerminalNo='''+@t+''' and Mileage>0'
    end   
    SET @SQL='insert into #temptb(TerminalNo,MinMileage,MaxMileage)SELECT tabA.TerminalNo,tabB.Mileage as MinMileage,tabC.Mileage as MaxMileage
FROM
    (SELECT TerminalNo,MIN(id) as MinId,MAX(id) as MaxId
    FROM '+@GpsPath+'
    WHERE 1=1 '+@WHERES+' 
    GROUP BY TerminalNo) tabA
LEFT JOIN '+@GpsPath+' tabB ON tabA.MinId=tabB.Id
LEFT JOIN '+@GpsPath+' tabC ON tabA.MaxId=tabC.Id'    
    Exec(@SQL) 
    set @number=@number+1
    set @S=DATEADD(d,1,@S)
    set @E=DATEADD(d,1,@E)
    end
select ROW_NUMBER() Over(order by CompanyName desc) as rowId,#temptb.TerminalNo,CarNo,CompanyName,SUM(#temptb.MaxMileage-#temptb.MinMileage)as Mileage
from #temptb
inner join dbo.Terminal on #temptb.TerminalNo=dbo.Terminal.TerminalNo
    inner join dbo.Car on dbo.Terminal.CarId=dbo.Car.CarId
    inner join dbo.Company on dbo.Car.CompanyId=dbo.Company.CompanyId 
where  EXISTS (select TerminalNo from #temptb where EXISTS (select CarId from Car where CompanyId=@CompanyId and CarId=Terminal.CarId) and #temptb.TerminalNo=Terminal.TerminalNo) OR #temptb.TerminalNo=@t
--where dbo.Company.CompanyId=@CompanyId OR #temptb.TerminalNo=@t
group by #temptb.TerminalNo,CarNo,CompanyName
        
if object_id('tempdb..#temptb')>0
drop table #temptb

select @end_date = getdate() select datediff(ms,@begin_date,@end_date) as '用时/毫秒'

计划任务:

USE [SGPS_SZ] 
GO
-- =============================================
-- Author:        <Author,,YangSongKai>
-- Create date: <Create Date,2012-07-25 09:18:03,>
-- Description:    <Description,计划任务-统计里程,>
-- =============================================

declare @GpsPath varchar(30)
declare @StartTime varchar(20)--开始时间(时分秒)
declare @EndTime varchar(20)--结束时间
declare @S varchar(20)--开始时间
declare @E varchar(20)--结束时间
declare @CurrentTime datetime--表中最后一条数据的时间
declare @result int--相差天数
declare @number int--循环种子

if object_id('[StatisticsMileage]') is null --如果表不存在,则创建表
begin
create table  StatisticsMileage
(
    rowId int IDENTITY (1,1) PRIMARY KEY ,
    TerminalNo varchar(20),
    CarNo varchar(20),
    [date]datetime,
    CompanyName varchar(30),
    Mileage decimal(18,2)
)
end
set @number=0
set @CurrentTime=(select MAX([date]) from StatisticsMileage)--获取数据库中的最大时间
set @CurrentTime=DATEADD(d,1,@CurrentTime)
if(@CurrentTime is null or @CurrentTime='')
set @CurrentTime='2012-07-20'--这里设置:如果数据库为空,统计的开始时间
set @result=DATEDIFF(d,@CurrentTime,GETDATE())--只能计算本月内相差的天数
 while(@number<=@result)
 begin
    set @GpsPath='dbo.GpsPath'+CONVERT(VARCHAR(10),@CurrentTime,112)--动态数据表
    set @StartTime=' 08:00:00'
    set @EndTime=' 20:00:00'
    set @S=CONVERT(varchar(20),@CurrentTime,23)+@StartTime
    set @E=CONVERT(varchar(20),@CurrentTime,23)+@EndTime
    exec('insert into StatisticsMileage(TerminalNo,CarNo,date,CompanyName,Mileage)select a.TerminalNo,CarNo,date,CompanyName,SUM(a.MaxMileage-a.MinMileage)as Mileage
from 
(SELECT tabA.TerminalNo,tabB.Mileage as MinMileage,tabC.Mileage as MaxMileage,date
FROM
    (SELECT TerminalNo,MIN(id) as MinId,MAX(id) as MaxId,convert(varchar(8),max(GpsTime),112) date
    FROM '+@GpsPath+' 
    WHERE 1=1  and GpsTime between '''+@S+''' and '''+@E+'''   
    GROUP BY TerminalNo) tabA
    LEFT JOIN '+@GpsPath+' tabB ON tabA.MinId=tabB.Id
    LEFT JOIN '+@GpsPath+' tabC ON tabA.MaxId=tabC.Id)a
    inner join dbo.Terminal on a.TerminalNo=dbo.Terminal.TerminalNo
    inner join dbo.Car on dbo.Terminal.CarId=dbo.Car.CarId
    inner join dbo.Company on dbo.Car.CompanyId=dbo.Company.CompanyId 
group by a.TerminalNo,CarNo,CompanyName,date')
    --种子和天数+1
    set @number=@number+1
    set @CurrentTime=DATEADD(d,1,@CurrentTime)
end

 

posted @ 2012-08-17 15:57  Archosaur  Views(262)  Comments(0Edit  收藏  举报