存储过程之-------计算篇

 1 CREATE   PROCEDURE   HVC_Statistics_Sta
 2 (
 3    @week   int
 4 )
 5
 6 AS
 7   set  nocount  on
 8
 9    if   not   exists ( select   *   from  HVC_Statistics_PortalRank  where  week =   @week )
10      begin
11        insert   into  HVC_Statistics_PortalRank(PortalID,TotalAccessed,CreatedDate,Week)
12         select  PortalID,HitCount, getdate (), @week
13         from  HVC_Counter_Counter
14     end
15    if   exists ( select   *   from  HVC_Statistics_PortalRank  where  week =   @week )
16      begin
17      update  HVC_Statistics_PortalRank  set  
18     AllArticlesNum =  ( SELECT   count ( * FROM  HVC_News_Article B
19                  WHERE  HVC_Statistics_PortalRank.PortalID  = B.PortalID  AND   B.Passed  =   1 ),
20     AllArticleHits =  ( SELECT   isnull ( sum (C.Hits), 0 )   FROM  HVC_News_Article C
21                  WHERE  HVC_Statistics_PortalRank.PortalID  = C.PortalID  AND   C.Passed  =   1 ),
22     WeekArticleUpdated =  ( SELECT   count ( * FROM  HVC_News_Article F
23                  WHERE  HVC_Statistics_PortalRank.PortalID  = F.PortalID  AND   F.Passed  =   1   AND   DateDiff (w, F.CreatedDate, GetDate ()) = 0 ),
24     WeekArticleHits = ( SELECT    isnull ( sum (G.Hits), 0 )   FROM  HVC_News_Article G
25                  WHERE  HVC_Statistics_PortalRank.PortalID  = G.PortalID  AND   G.Passed  =   1   AND   DateDiff (w, G.CreatedDate, GetDate ()) = 0 ),
26
27     MonthTotalUpdated =  ( SELECT   count ( * FROM  HVC_News_Article D
28                  WHERE  HVC_Statistics_PortalRank.PortalID  = D.PortalID  AND   D.Passed  =   1   AND   DateDiff (m, D.CreatedDate, GetDate ()) = 0 ),
29     MonthArticleHits = ( SELECT   isnull ( sum (E.Hits), 0 FROM  HVC_News_Article E
30                  WHERE  HVC_Statistics_PortalRank.PortalID  = E.PortalID  AND   E.Passed  =   1   AND   DateDiff (m, E.CreatedDate, GetDate ()) = 0 )
31     
32      end
33 RETURN
34
35
36 GO
37
38 if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' {databaseOwner}[{objectQualifier}HVC_Statistics_Cal] ' and   OBJECTPROPERTY (id, N ' IsProcedure ' =   1 )
39 drop   procedure  {databaseOwner}{objectQualifier}HVC_Statistics_Cal
40 GO
41
42 CREATE   PROCEDURE  HVC_Statistics_Cal
43 (
44    @week   int
45 )
46
47 AS
48
49 if   exists ( select   *   from  HVC_Statistics_PortalRank  where  week =   @week )
50      begin
51      update  HVC_Statistics_PortalRank  set  
52     ResultValue =  
53 ((HVC_Statistics_PortalRank.TotalAccessed * ( SELECT  FloatValue  FROM  HVC_Statistics_Setting  WHERE  AttrID  = 1 ))
54 + ( AllArticlesNum * ( SELECT  FloatValue  FROM  HVC_Statistics_Setting  WHERE  AttrID  = 2 ))
55 + ( AllArticleHits * ( SELECT  FloatValue  FROM  HVC_Statistics_Setting  WHERE  AttrID  = 3 ))
56 + ( MonthTotalUpdated * ( SELECT  FloatValue  FROM  HVC_Statistics_Setting  WHERE  AttrID  = 4 ))
57 + ( MonthArticleHits * ( SELECT  FloatValue  FROM  HVC_Statistics_Setting  WHERE  AttrID  = 5 ))
58 + ( WeekArticleUpdated * ( SELECT  FloatValue  FROM  HVC_Statistics_Setting  WHERE  AttrID  = 6 ))
59 + ( WeekArticleHits * ( SELECT  FloatValue  FROM  HVC_Statistics_Setting  WHERE  AttrID  = 7 ))
60 + ( Score * ( SELECT  FloatValue  FROM  HVC_Statistics_Setting  WHERE  AttrID  = 8 )))
61
62 end
63
64 RETURN
65
66
67 GO
68
69

你可能感兴趣的:(存储过程)