查找每天,每周和每月的平均值

时间:2018-07-11 14:24:54

标签: sql-server average

我在下面的查询中显示了带有分配日期和其他列的索赔清单。

我想找到-
平均而言,每天,每周和每月从现场向OpenTemp发送多少作业
,任何想法都将不胜感激。在此先感谢

    SELECT 
         A.[ClaimID]
          ,[ClaimNum]
          ,B.AssignedDate
          ,[ClaimReOpenDate]
          ,[ClaimOriginalReOpenDate]
           ,[AdjusterName]
          ,[Specialty]
          ,[Office]
          ,[Team]
          ,[Supervisor]
          ,[OpenforRD]
          ,[MaxRdDate]
          ,[MinRdDate]
          ,[CatCode]
      FROM tableA a
    left outer join  TableB b on A.ClaimID = B.ClaimID
      where ToUser like '%RD%'
      and FromUser like '%Field%'
     AND Specialty = 'RD' and RowIsCurrent  = 'Y'
      and DerivedClaimStatus in ('Open', 'Re-Open')

样本数据

    ClaimID ClaimNum    AssignedDate        ClaimReOpenDate ClaimOriginalReOpenDate AdjusterName    Specialty   Office  Team            Supervisor  OpenforRD       MaxRdDate       MinRdDate   CatCode

    2334582 2334582     2018-06-22 10:52:51.283 NULL            NULL                Byers           RD          Akron   Akron RD Team 1  Tilley         1           2018-06-22      2018-06-22  NULL

    2273950 2273950     2018-02-05 11:49:07.933 NULL            NULL                Cannon          RD          Akron   Akron RD Team 2  Ailing         1           2018-02-05      2018-02-05  NULL

    2333064 2333064     2018-06-27 09:06:10.857 NULL            NULL                Murphy          RD          Akron   Akron RD Team 1  Tilley         1           2018-06-27      2018-06-27  NULL

    2303323 2303323     2018-03-29 13:49:48.730 NULL            NULL                Rothermel       RD          Akron   Akron RD Team 2  Ailing         1           2018-03-29      2018-03-29  NULL

1 个答案:

答案 0 :(得分:0)

        --  for week                                                              
       Select Avg(ClaimCount) from (             Select Count(1) AS ClaimCount  from    
       FROM tableA a
            left outer join  TableB b on A.ClaimID = B.ClaimID
            where ToUser like '%RD%'
  and FromUser like '%Field%'
 AND Specialty = 'RD' and RowIsCurrent  = 'Y'
  and DerivedClaimStatus in ('Open', 'Re-Open') 
   Group by  DATEPart(WK,AssignedDate))i                                             
      --- for month                                                                 
   Select Avg(ClaimCount) from (             Select Count(1) AS ClaimCount  from    
       FROM tableA a
            left outer join  TableB b on A.ClaimID = B.ClaimID
            where ToUser like '%RD%'
  and FromUser like '%Field%'
 AND Specialty = 'RD' and RowIsCurrent  = 'Y'
  and DerivedClaimStatus in ('Open', 'Re-Open') 
   Group by  DATEPart(MONTH,AssignedDate))i