0

我正在根据年初和年末(在本例中为 2013 年)成功计算出员工 12 个月的平均销售额。但是,当员工在年初之后开始工作时,他们的平均值必须从雇用日期开始计算。我需要弄清楚如何计算雇用日期在年初之后开始的员工的 12 个月平均值。

到目前为止,这是我当前的查询:

    DECLARE @begDt DATETIME, @endDt DATETIME
    SET @begDt = DATEADD(YEAR, DATEDIFF(YEAR, 0,DATEADD(YEAR, -1, GETDATE())), 0) 
    SET @endDt = DATEADD(MILLISECOND, -3, DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(YEAR, -1, GETDATE())) + 1, 0))

  select
  [Account Executive] = v.Name_AccountExecutive
, [YTD Production] = cast(sum (case when v.Process_FundingDt between @begDt and @endDt then v.Loan_LoanAmt else 0 end) as int)
, [Monthly Average] = cast(sum (case when v.Process_FundingDt between @begDt and @endDt then v.Loan_LoanAmt else 0 end) 
  / (case when @begDt = '1/1/2012' then 
      case when v.Name_AccountExecutive = 'HIEB, BRANDON' then 7 when v.Name_AccountExecutive = 'AMORATI, KAREN' then 8 else 12 end
      else (datepart(m, @endDt)) end) as int)
from LOS.LoanView v
group by v.Name_AccountExecutive
4

1 回答 1

0

嘿,伙计们只是想回答这个问题,因为我没有正确地提出问题,而且看起来很混乱,但这就是我这样做的方式。如果有更好的方法,请随时发表评论,但这暂时有效。

, [Monthly Average] = cast(sum (case when v.Process_FundingDt between @begDt and @endDt then v.Loan_LoanAmt else 0 end) as int)
/(case when aa.End_Date is NULL then 
      (
      case when Year(aa.Hire_Date)< Year(@begDt) and Year(@begDt)<Year(getdate()) then 12 -- hired anytime before the currrent reporting year and current reporting year is not current year.
            when Year(aa.Hire_Date) < Year(@begDt) and Year(@begDt)=Year(getdate()) then MONTH(getdate())-- hired  anytime before the currrent reporting year  and current reporting year is the current year
            when Year(aa.Hire_Date) = Year(@begDt) and Year(@begDt)<Year(getdate()) then 12-Month(aa.Hire_Date) -- hired same as reporting year but the reporting year is not same as current year
            when Year(aa.Hire_Date) = Year(@begDt) and Year(@begDt)=Year(getdate()) then MONTH(getdate())-Month(aa.Hire_Date) -- hired same as reporting year and the reporting year is  same as current year
      end
      )
      else
      (
      Case when Year(aa.End_Date)<Year(@begDt)  then NULL
            when Year(aa.End_Date)=Year(@begDt) and YEAR(aa.Hire_Date)<Year(aa.End_Date)  then MONTH(aa.End_Date) -- end date is same as reporting year, was hired preivous year, current year doesn't matter
            when Year(aa.End_Date)=Year(@begDt)and YEAR(aa.Hire_Date)=Year(aa.End_Date)  then MONTH(aa.End_Date)-MONTH(aa.Hire_Date) -- end year, start year and reporting year is same, current year doesn't matter
            when  Year(aa.End_Date)>Year(@begDt) and YEAR(aa.Hire_Date)=Year(@begDt) then 12-MONTH(aa.Hire_Date)
            when  Year(aa.End_Date)>Year(@begDt) and YEAR(aa.Hire_Date)<Year(@begDt) then 12
      end
      )
      end
)
于 2013-05-31T18:35:15.317 回答