自己寫的一個流量分析(用存儲過程實現)

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

/*
後端流量統計
*/
Create procedure [dbo].[P_SiteVisitCount]
as
begin
 set nocount on
 declare @FirstDate varchar(10),@Today varchar(10)
 declare @TempFirstDate datetime,@TempToday datetime
 declare @ThisDayVisit int ,  @MaxVisitDay varchar(20)
 declare @ThisMonthVisit int ,  @MaxVisitMonth varchar(20)
 declare @ThisYearVisit int ,  @MaxVisitYear varchar(20)
 declare @VisitCount int
 declare @AvgDayVisit int , @AvgMonthVisit int, @AvgYearVisit int
 declare @Days int , @Months int , @Years int
 
 select @TempFirstDate = min(VisitDate) from T_SiteVisit
 set @TempToday = getdate()

 set @FirstDate = convert(varchar,@TempFirstDate,111) --啟用日
 set @Today = convert(varchar,@TempToday,111)   --本日
 select @VisitCount = sum(VisitCount) from T_SiteVisit --總訪問數
 

 set @Days = datediff(day,@FirstDate,@Today) + 1
 set @Months = datediff(month,@FirstDate,@Today) + 1
 set @Years = datediff(year,@FirstDate,@Today) + 1
 
 
 set @AvgDayVisit = @VisitCount/@Days  --平均日訪數
 set @AvgMonthVisit = @VisitCount/@Months --平均月訪數
 set @AvgYearVisit = @VisitCount/@Years  --平均年訪數

 --本日訪問數
 select @ThisDayVisit = sum(VisitCount) from T_SiteVisit
 where convert(varchar,VisitDate,111) = convert(varchar,getdate(),111)

 --本月訪問數
 select @ThisMonthVisit = sum(VisitCount) from T_SiteVisit
 where datepart(year,VisitDate) = datepart(year,getdate()) and
 datepart(month,VisitDate) = datepart(month,getdate())

 --本年訪問數
 select @ThisYearVisit = sum(VisitCount) from T_SiteVisit
 where datepart(year,VisitDate) = datepart(year,getdate())


 --最大日訪問數
 select top 1 @MaxVisitDay = convert(varchar,VisitCount) + '(' +
 convert(varchar,VisitDate,111) + ')' from T_SiteVisit order by VisitCount desc
 

 --最大月訪數
 select top 1 @MaxVisitMonth = convert(varchar,MonthVisit) + '(' + Date + ')'
 from (
 select  MonthVisit = sum(VisitCount),
 Date = convert(varchar,datepart(year,VisitDate)) + '/' + convert(varchar,datepart(month,VisitDate))
    from T_SiteVisit
 group by  convert(varchar,datepart(year,VisitDate)) + '/' + convert(varchar,datepart(month,VisitDate))
 ) T order by  MonthVisit desc

 --最大年訪數
 select top 1 @MaxVisitYear = convert(varchar,YearVisit) + '(' + Date + ')'
 from (
 select  YearVisit = sum(VisitCount),
 Date = convert(varchar,datepart(year,VisitDate)) from T_SiteVisit
 group by  datepart(year,VisitDate)
 ) T order by YearVisit desc

 select
 FirstDate = @FirstDate,
 Today = @Today,
 ThisDayVisit = @ThisDayVisit,
 ThisMonthVisit = @ThisMonthVisit,
 ThisYearVisit = @ThisYearVisit,
 AvgDayVisit = @AvgDayVisit,
 AvgMonthVisit = @AvgMonthVisit,
 AvgYearVisit = @AvgYearVisit, 
 MaxVisitDay = @MaxVisitDay,
 MaxVisitMonth = @MaxVisitMonth,
 MaxVisitYear = @MaxVisitYear,
 VisitCount = @VisitCount

 select  No = row_number()over(order by T.MenuID),T.*  from (
 select distinct T1.MenuID,
 SitePathName = dbo.F_GetSiteNameByMenuID(T1.MenuID),
 ThisMonthVisit = T3.VisitCount,
 VisitCount = T2.VisitCount from T_UnitVisit
 T1 inner join
 (
  select MenuID,VisitCount=sum(VisitCount) from T_UnitVisit group by MenuID
 ) T2 on T1.MenuID = T2.MenuID
 inner join
 (
  select MenuID,VisitCount=sum(VisitCount) from T_UnitVisit
  where datepart(year,VisitDate) = datepart(year,getdate()) and
  datepart(month,VisitDate) = datepart(month,getdate())
  group by  MenuID,
  datepart(year,VisitDate),
  datepart(month,VisitDate)
 ) T3 on T1.MenuID = T3.MenuID
 )T
 order by T.MenuID
 
end

表結构如下:
/****** 物件:  Table [dbo].[T_SiteVisit]    指令碼日期: 05/20/2008 11:17:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[T_SiteVisit](
 [VisitId] [int] IDENTITY(1,1) NOT NULL,
 [VisitDate] [datetime] NOT NULL,
 [VisitCount] [int] NOT NULL CONSTRAINT [DF_T_SiteVisit_VisitCount]  DEFAULT ((0)),
 CONSTRAINT [PK_T_SiteVisit] PRIMARY KEY CLUSTERED
(
 [VisitId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

posted on 2008-05-20 11:23  Harlan---  阅读(1009)  评论(1编辑  收藏  举报

导航