我正在根据年初和年末(在本例中为 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