Home Database Mysql Tutorial 页面 PV 按照 年 月 日 小时 统计 的存储过程

页面 PV 按照 年 月 日 小时 统计 的存储过程

Jun 07, 2016 pm 02:55 PM
storage Hour statistics process need page

需求:需要做一个统计 网站是这样的:网站上有 视频频道、图片频道、新闻频道等 需要做一个统计,能够统计这几个频道 中每个资源 某个小时、 某天、某周、某月、某年、总的 访问的次数 从上述需求看,只要统计每个资源,一天24 个小时的访问量 然后分类汇总就

需求:需要做一个统计
网站是这样的:网站上有  视频频道、图片频道、新闻频道  等

需要做一个统计,能够统计这几个频道 中每个资源 某个小时、 某天、某周、某月、某年、总的 访问的次数

从上述需求看,只要统计每个资源,一天24 个小时的访问量 然后分类汇总就可以 算出  某天、某周、某月、某年、总的 范围的次数

原理就是这样。


表结构如下:
[Channel] 频道表
ID
Name    //频道名称

[Video] 视频表
ID
...

[Photo] 图库表
ID
... 

[News]  新闻表
ID
... 


存储过程用了下面的两张表详细的写下:

[PV] 表
[ID] [int] IDENTITY(1,1) NOT NULL,  [ChannelID] [int] NOT NULL,//频道ID [SourceID] [int] NOT NULL,//源ID [Times] [int] NOT NULL,//次数 [Y] [smallint] NULL,//年  如  2000  [M] [tinyint] NULL,//月   如  12 [W] [tinyint] NULL,//周  如  50  [D] [tinyint] NULL,//日   如  21 [H] [tinyint] NULL//小时 如  16


[PVS]  汇总结果表 [ID] [int] IDENTITY(1,1) NOT NULL, [ChannelID] [int] NOT NULL,//频道ID [SourceID] [int] NOT NULL,//源ID [HourRate] [float] NULL,//当前小时与上个小时相比上升的速率 [HourTimes] [int] NULL,//当前小时访问的次数 [DayRate] [float] NULL,//今天与昨天相比上升的速率 [DayTimes] [int] NULL,//今天访问的次数 [WeekRate] [float] NULL,//当周与上周相比上升的速率 [WeekTimes] [int] NULL,//当周访问的次数 [MonthRate] [float] NULL,//当月与上月相比上升的速率 [MonthTimes] [int] NULL,//当周访问的次数 [YearRate] [float] NULL,//今年与上一年相比上升的速率 [YearTimes] [int] NULL,//今年访问的次数 [Total] [int] NULL//访问的总次数






-- =============================================
-- Author:		<Author,FHZ,>
-- Create date: <Create Date,2011-11-25 15:55,>
-- Description:	<Description,更新统计信息,>
-- =============================================
CREATE proc [dbo].[procCountPV](
@ChannelID nvarchar(50),
@SourceID int
)
as
begin
	declare @TEMID int; --临时ID
	declare @Now datetime;
	set @Now = GETDATE();
	
	declare @Y smallint;--年
	declare @M tinyint;--月
	declare @W tinyint;--周
	declare @D tinyint;--日
	declare @H tinyint;--小时
	
	set @Y = DATEPART(YY,@Now);
	set @M = DATEPART(MM,@Now);
	set @W = DATEPART(WW,@Now);
	set @D = DATEPART(DD,@Now);
	set @H = DATEPART(HH,@Now);
	
	
	select @TEMID = [ID] from [PV] where [ChannelID] = @ChannelID and [SourceID]=@SourceID and [Y] = @Y and [M]=@M and [D]=@D and [H] = @H;
	
	if @TEMID is null
		Insert into [PV]([ChannelID],[SourceID],[Times],[Y],[M],[W],[D],[H]) values(@ChannelID ,@SourceID,1,@Y,@M,@W,@D,@H);
	else
		Update [PV] set [Times] = [Times]+1 where [ID]= @TEMID;		
	
	/*计算现在*/
	Declare @NowHourTimes int;
	Declare @NowDayTimes int;
	Declare @NowWeekTimes int;
	Declare @NowMonthTimes int;
	Declare @NowYearTimes int;
	
	--Y M D H
	select @NowHourTimes = SUM([Times]) from [PV] where  ChannelID = @ChannelID and SourceID = @SourceID and [Y] = @Y and [M]=@M and [D]=@D and [H] = @H;	
	
	--Y M D
	select @NowDayTimes = SUM([Times]) from [PV] where  ChannelID = @ChannelID and SourceID = @SourceID and [Y] = @Y and [M]=@M and [D]=@D;	
		
	--Y W
	select @NowWeekTimes = SUM([Times]) from [PV] where  ChannelID = @ChannelID and SourceID = @SourceID and [Y] = @Y and [W]=@W;
	
	--Y M
	select @NowMonthTimes = SUM([Times]) from [PV] where  ChannelID = @ChannelID and SourceID = @SourceID and [Y] = @Y and [M]=@M;
	
	--Y
	select @NowYearTimes = SUM([Times]) from [PV] where  ChannelID = @ChannelID and SourceID = @SourceID and [Y] = @Y;
	
	
	if @NowHourTimes is null
		set @NowHourTimes = 0;
	
	if @NowDayTimes is null
		set @NowDayTimes = 0;

	if @NowWeekTimes is null
		set @NowWeekTimes = 0;

	if @NowMonthTimes is null
		set @NowMonthTimes = 0;

	if @NowYearTimes is null
		set @NowYearTimes = 0;
	
	
	
	
	/*计算之前*/
	Declare @PreHourTimes int;
	Declare @PreDayTimes int;
	Declare @PreWeekTimes int;
	Declare @PreMonthTimes int;
	Declare @PreYearTimes int;
	
	
	
	--Y M D H
	Declare @PreHourDateTime datetime;
	set @PreHourDateTime = DATEADD(HH,-1,@Now);
	
	select @PreHourTimes = SUM([Times]) from [PV] where  ChannelID = @ChannelID and SourceID = @SourceID and [Y] = DATEPART(YY,@PreHourDateTime) and [M]=DATEPART(MM,@PreHourDateTime) and [D]=DATEPART(DD,@PreHourDateTime) and [H] = DATEPART(HH,@PreHourDateTime);	
	
	--Y M D
	Declare @PreDayDateTime datetime;
	set @PreDayDateTime = DATEADD(DD,-1,@Now);
	
	select @PreDayTimes = SUM([Times]) from [PV] where  ChannelID = @ChannelID and SourceID = @SourceID and [Y] = DATEPART(YY,@PreDayDateTime) and [M]=DATEPART(MM,@PreDayDateTime) and [D]=DATEPART(DD,@PreDayDateTime);	
		
	--Y W
	Declare @PreWeekDateTime datetime;
	set @PreWeekDateTime = DATEADD(WW,-1,@Now);

	select @PreWeekTimes = SUM([Times]) from [PV] where  ChannelID = @ChannelID and SourceID = @SourceID and [Y] =  DATEPART(YY,@PreWeekDateTime) and [W]= DATEPART(WW,@PreWeekDateTime);
	
	--Y M
	Declare @PreMonthDateTime datetime;
	set @PreMonthDateTime = DATEADD(MM,-1,@Now);
	select @PreMonthTimes = SUM([Times]) from [PV] where  ChannelID = @ChannelID and SourceID = @SourceID and [Y] = DATEPART(YY,@PreMonthDateTime) and [M]= DATEPART(MM,@PreMonthDateTime);
	
	--Y
	select @PreYearTimes = SUM([Times]) from [PV] where  ChannelID = @ChannelID and SourceID = @SourceID and [Y] = @Y - 1;
	
	
	if @PreHourTimes is null
		set @PreHourTimes = 0;
	
	if @PreDayTimes is null
		set @PreDayTimes = 0;

	if @PreWeekTimes is null
		set @PreWeekTimes = 0;

	if @PreMonthTimes is null
		set @PreMonthTimes = 0;

	if @PreYearTimes is null
		set @PreYearTimes = 0;

	
	declare @HourRate float;
	declare @DayRate float;
	declare @WeekRate float;
	declare @MonthRate float;
	declare @YearRate float;
	
	set @HourRate = 0;
	set @DayRate = 0;
	set @WeekRate = 0;
	set @MonthRate = 0;
	set @YearRate = 0;
	
	if @PreHourTimes > 0 
		set @HourRate = ( @NowHourTimes - @PreHourTimes )/ (@PreHourTimes+0.0);	
	
	if @PreDayTimes > 0 
		set @DayRate = ( @NowDayTimes - @PreDayTimes )/ (@PreDayTimes+0.0);
	
	if @PreWeekTimes > 0 
		set @WeekRate = ( @NowWeekTimes - @PreWeekTimes )/ (@PreWeekTimes+0.0);
		
	if @PreMonthTimes > 0 
		set @MonthRate = ( @NowMonthTimes - @PreMonthTimes )/ (@PreMonthTimes+0.0);
		
	if @PreYearTimes > 0 
		set @YearRate = ( @NowYearTimes - @PreYearTimes )/ (@PreYearTimes+0.0);
		


	
	
	/*计算总量*/
	declare @Total int;
	select @Total = SUM([Times]) From [PV] where ChannelID = @ChannelID and SourceID = @SourceID;
	if @Total is null
		set @Total = 0;	
	
	declare @TempID int;
	set @TempID = null;
	
	/*操作CountSummary*/	
	Select @TempID = ID from [PVS]	where ChannelID = @ChannelID and SourceID = @SourceID;
	if @TempID is null 		
		Insert into [PVS]([ChannelID],[SourceID],[HourRate],[HourTimes],[DayRate],[DayTimes],[WeekRate],[WeekTimes],[MonthRate],[MonthTimes],[YearRate],[YearTimes],[Total]) 
		Values(@ChannelID,@SourceID,@HourRate,@NowHourTimes,@DayRate,@NowDayTimes,@WeekRate,@NowWeekTimes,@MonthRate,@NowMonthTimes,@YearRate,@NowYearTimes,@Total);
	else		
		Update [PVS] set [HourRate]=@HourRate,[HourTimes]=@NowHourTimes,[DayRate]=@DayRate,[DayTimes]=@NowDayTimes,[WeekRate]=@WeekRate,[WeekTimes]=@NowWeekTimes,[MonthRate]=@MonthRate,[MonthTimes]=@NowMonthTimes,[YearRate]=@YearRate,[YearTimes]=@NowYearTimes,[Total]=@Total where ID = @TempID;		
end

GO

Copy after login
CREATE TABLE [dbo].[PV](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[ChannelID] [int] NOT NULL,
	[SourceID] [int] NOT NULL,
	[Times] [int] NOT NULL,
	[Y] [smallint] NULL,
	[M] [tinyint] NULL,
	[W] [tinyint] NULL,
	[D] [tinyint] NULL,
	[H] [tinyint] NULL,
 CONSTRAINT [PK_PV] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[PV] ADD  CONSTRAINT [DF_PV_Times]  DEFAULT ((0)) FOR [Times]
GO

ALTER TABLE [dbo].[PV] ADD  CONSTRAINT [DF_PV_Y]  DEFAULT ((2000)) FOR [Y]
GO

ALTER TABLE [dbo].[PV] ADD  CONSTRAINT [DF_PV_M]  DEFAULT ((1)) FOR [M]
GO

ALTER TABLE [dbo].[PV] ADD  CONSTRAINT [DF_PV_W]  DEFAULT ((1)) FOR [W]
GO

ALTER TABLE [dbo].[PV] ADD  CONSTRAINT [DF_PV_D]  DEFAULT ((1)) FOR [D]
GO

ALTER TABLE [dbo].[PV] ADD  CONSTRAINT [DF_PV_H]  DEFAULT ((0)) FOR [H]
GO

Copy after login
CREATE TABLE [dbo].[PVS](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[ChannelID] [int] NOT NULL,
	[SourceID] [int] NOT NULL,
	[HourRate] [float] NULL,
	[HourTimes] [int] NULL,
	[DayRate] [float] NULL,
	[DayTimes] [int] NULL,
	[WeekRate] [float] NULL,
	[WeekTimes] [int] NULL,
	[MonthRate] [float] NULL,
	[MonthTimes] [int] NULL,
	[YearRate] [float] NULL,
	[YearTimes] [int] NULL,
	[Total] [int] NULL,
 CONSTRAINT [PK_PVS] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to copy a page in Word How to copy a page in Word Feb 20, 2024 am 10:09 AM

Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

Outlook signature disappears every day after restart Outlook signature disappears every day after restart Feb 19, 2024 pm 05:24 PM

An email signature is important to demonstrate legitimacy and professionalism and includes contact information and company logo. Outlook users often complain that signatures disappear after restarting, which can be frustrating for those looking to increase their company's visibility. In this article, we will explore different fixes to resolve this issue. Why do my Microsoft Outlook signatures keep disappearing? If this is your first time using Microsoft Outlook, make sure your version is not a trial version. Trial versions may cause signatures to disappear. Additionally, the version architecture should also match the version architecture of the operating system. If you find that your email signature disappears from time to time in Outlook Web App, it may be due to

Vue3+TS+Vite development skills: how to encrypt and store data Vue3+TS+Vite development skills: how to encrypt and store data Sep 10, 2023 pm 04:51 PM

Vue3+TS+Vite development tips: How to encrypt and store data. With the rapid development of Internet technology, data security and privacy protection are becoming more and more important. In the Vue3+TS+Vite development environment, how to encrypt and store data is a problem that every developer needs to face. This article will introduce some common data encryption and storage techniques to help developers improve application security and user experience. 1. Data Encryption Front-end Data Encryption Front-end encryption is an important part of protecting data security. Commonly used

Huawei will launch innovative MED storage products next year: rack capacity exceeds 10 PB and power consumption is less than 2 kW Huawei will launch innovative MED storage products next year: rack capacity exceeds 10 PB and power consumption is less than 2 kW Mar 07, 2024 pm 10:43 PM

This website reported on March 7 that Dr. Zhou Yuefeng, President of Huawei's Data Storage Product Line, recently attended the MWC2024 conference and specifically demonstrated the new generation OceanStorArctic magnetoelectric storage solution designed for warm data (WarmData) and cold data (ColdData). Zhou Yuefeng, President of Huawei's data storage product line, released a series of innovative solutions. Image source: Huawei's official press release attached to this site is as follows: The cost of this solution is 20% lower than that of magnetic tape, and its power consumption is 90% lower than that of hard disks. According to foreign technology media blocksandfiles, a Huawei spokesperson also revealed information about the magnetoelectric storage solution: Huawei's magnetoelectronic disk (MED) is a major innovation in magnetic storage media. First generation ME

How to implement data statistics and analysis in uniapp How to implement data statistics and analysis in uniapp Oct 24, 2023 pm 12:37 PM

How to implement data statistics and analysis in uniapp 1. Background introduction Data statistics and analysis are a very important part of the mobile application development process. Through statistics and analysis of user behavior, developers can have an in-depth understanding of user preferences and usage habits. Thereby optimizing product design and user experience. This article will introduce how to implement data statistics and analysis functions in uniapp, and provide some specific code examples. 2. Choose appropriate data statistics and analysis tools. The first step to implement data statistics and analysis in uniapp is to choose the appropriate data statistics and analysis tools.

Implementation of linear and pie chart functions in Vue statistical charts Implementation of linear and pie chart functions in Vue statistical charts Aug 19, 2023 pm 06:13 PM

The linear and pie chart functions of Vue statistical charts are implemented in the field of data analysis and visualization. Statistical charts are a very commonly used tool. As a popular JavaScript framework, Vue provides convenient methods to implement various functions, including the display and interaction of statistical charts. This article will introduce how to use Vue to implement linear and pie chart functions, and provide corresponding code examples. Linear graph function implementation A linear graph is a type of chart used to display trends and changes in data. In Vue, we can use some excellent

How to customize and edit standby mode on iPhone: What's new in iOS 17 How to customize and edit standby mode on iPhone: What's new in iOS 17 Sep 21, 2023 pm 04:01 PM

Standby is a new feature in the iOS 17 update that provides a new and enhanced way to access information when your phone is idle quickly. With StandBy, you can conveniently check the time, view upcoming events, browse your calendar, get weather updates for your location, and more. Once activated, the iPhone will intuitively enter standby mode when set to landscape while charging. This feature is perfect for wireless charging points like your bedside table, or when you're away from your iPhone charging during daily tasks. It allows you to swipe through various widgets displayed in standby to access different sets of information from various applications. However, you may want to modify these widgets or even delete some based on your preferences and the information you need frequently. So let's dive into

How to quickly refresh a web page? How to quickly refresh a web page? Feb 18, 2024 pm 01:14 PM

Page refresh is very common in our daily network use. When we visit a web page, we sometimes encounter some problems, such as the web page not loading or displaying abnormally, etc. At this time, we usually choose to refresh the page to solve the problem, so how to refresh the page quickly? Let’s discuss the shortcut keys for page refresh. The page refresh shortcut key is a method to quickly refresh the current web page through keyboard operations. In different operating systems and browsers, the shortcut keys for page refresh may be different. Below we use the common W

See all articles