我在使用SQL编写报表方面相当新,并且遇到%计算问题。按部门和员工的查询分组,包括YTDAccrued,YTDUsed和Reversing的计算。反转是我遇到问题的计算(YTDUsed / YTDAccrued)。按员工分组计算罚款是按部门计算的组,似乎没有正常工作,它是添加所有员工%计算而不是仅为部门进行单独计算。如何让组“部门”正确计算?我附上了结果图片。
DECLARE @calyr varchar(4) Select @Calyr = '2014'
SELECT asi_ptoytdbalances.Calyr,
Department,
asi_ptoytdbalances.EmpID,
asi_ptoytdbalances.NameLast,
asi_ptoytdbalances.NameFirst,
SUM(CASE WHEN asi_PTOytdBalances.Description = 'Monthly Earned PTO' THEN Round((PTO),2) ELSE 0.00 END) AS YTDAccrued,
Sum(CASE WHEN asi_PTOytdBalances.Description = 'PTO Used' THEN Round((PTO),2) * -1 ELSE 0.00 END)AS YTDUsed,
Round((Sum(CASE WHEN asi_PTOytdBalances.Description = 'PTO Used' THEN PTO * -1 END)/SUM(CASE WHEN asi_PTOytdBalances.Description = 'Monthly Earned PTO' THEN PTO END)),4)AS Reversing
FROM ASI_PTOytdBalances
WHERE Calyr= @calyr
AND Description <> ''
GROUP BY asi_ptoytdbalances.Calyr,
asi_PTOytdBalances.Department,
asi_PTOytdBalances.EmpID,
asi_ptoytdbalances.NameLast,
asi_ptoytdbalances.NameFirst
ORDER BY asi_PTOytdBalances.Department,
asi_PTOytdBalances.EmpID