0

年の初めと終わりに基づいて、従業員の 12 か月の売上平均を正常に計算しています。この場合は 2013 年です。雇用日が年の初めより後に始まる従業員の 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 に答える