如何获取SQL HourlyPayRate列的平均薪水?

时间:2019-05-09 14:26:17

标签: sql sql-server sql-server-2016

我正在创建一个SSRS报告,我想获取我拥有的HourlyPayRate列的平均值(在我的案例中)。我是SQL的自学者,想知道如何在新表中(如果可能的话)获取HourlyPayRate的AVG。

DECLARE @PROCLEVEL varchar(6),
                   @HRYEAR varchar(5),
                           @HRPERIOD varchar(15)
SET @PROCLEVEL = 'XXX'
SET @HRYEAR = '2019'
SET @HRPERIOD = '03'
SELECT CASE
           WHEN HRPERIOD = 3 THEN 'March'
           WHEN HRPERIOD = 6 THEN 'June'
           WHEN HRPERIOD = 9 THEN 'September'
           WHEN HRPERIOD = 12 THEN 'DECEMBER'
       END HRPERIOD,
       EMPLOYEE,
       HRYEAR,
       PAY_RATE,
       FTE_TOTAL,
       JOB_CLASS_NAME,
       JOB_CLASS,
       SALARY_CLASS,
       CASE
           WHEN SALARY_CLASS = 'S'
                OR PAY_RATE > 1000 THEN (PAY_RATE / 2080)
           ELSE PAY_RATE
       END AS HourlyPayRate
FROM [LAWSONDWHR].[dbo].[RPT_EMPLOYEECENSUS_ASOF]
WHERE PROCESS_LEVEL = @PROCLEVEL
  AND HRYEAR = @HRYEAR
  AND HRPERIOD = @HRPERIOD
  AND JOB_CLASS = 'RN'
  AND FTE_TOTAL != 0
  AND MASTER_EMP_STATUS NOT IN ('ZZ',
                                'T1')
ORDER BY PAY_RATE

我正在做的是,当员工的薪水等级为薪水'S'时,或者如果薪水率大于1000,则取薪水率/ 2080一年的小时数。

3 个答案:

答案 0 :(得分:1)

如果要获取每个员工的平均小时工资,可以使用GROUP BY

SELECT Employee, AVG(HourlyPayRate) AS AvgHourlyRate
FROM (
    { your query }
) a
GROUP BY Employee

答案 1 :(得分:0)

这不是一个完整的解决方案,但是如果您使用的是TSQL(SQL Server),并且您愿意在所有员工一年中都工作相同小时的假设下运行(通常这并不是完全正确的假设,方式),您只需将HourlyPayRate包装在AVG()函数中...

答案 2 :(得分:0)

您可以不只是做某种形式的事情吗?我已经删除了变量和where子句,但可以根据需要进行调整:

Select Employee, AVG(HourlyPayRate)
from (
SELECT CASE
       WHEN HRPERIOD = 3 THEN 'March'
       WHEN HRPERIOD = 6 THEN 'June'
       WHEN HRPERIOD = 9 THEN 'September'
       WHEN HRPERIOD = 12 THEN 'DECEMBER'
   END HRPERIOD,
   EMPLOYEE,
   HRYEAR,
   PAY_RATE,
   FTE_TOTAL,
   JOB_CLASS_NAME,
   JOB_CLASS,
   SALARY_CLASS,
   CASE
       WHEN SALARY_CLASS = 'S'
            OR PAY_RATE > 1000 THEN (PAY_RATE / 2080)
       ELSE PAY_RATE
   END AS HourlyPayRate
FROM [LAWSONDWHR].[dbo].[RPT_EMPLOYEECENSUS_ASOF]
)
GROUP BY Employee