我正在尝试提取工资核算数据,需要在[PAYRATE]列中添加最大值和最小值以得出平均费率。该表可以有多行支票,具有相同的工资率,因此AVG [PAYRATE]将无效。我尝试了下面的代码,但它失败了。可以这样做吗?
(SELECT SUM((MAX(payrate))+(MIN(payrate)))
FROM PAYCHECKS
WHERE PAYROLCD in ('01', '02')
AND PYRLRTYP = 1
AND EMPLOYID = A.EMPLOYID
AND PYADNMBR = A.PYADNMBR) AS PAYRATE
无法对包含聚合或子查询的表达式执行聚合函数。
答案 0 :(得分:2)
删除外部sum
:
SELECT (MAX(payrate)+MIN(payrate))/2 AS PAYRATE
FROM PAYCHECKS
WHERE PAYROLCD in ('01', '02')
AND PYRLRTYP = 1
AND EMPLOYID = A.EMPLOYID
AND PYADNMBR = A.PYADNMBR
我为平均值添加了/2
。
答案 1 :(得分:0)
您需要对最小值和最大值进行平均值。此外,您需要除以小数2以获得准确的结果,否则如果除以2,则十进制结果将被截断。您可以根据下面的需要改变比例和精度。
SELECT ((MIN(payrate)+MAX(payrate))/CAST(2 AS DECIMAL (9,2)) AS PAYRATE
FROM PAYCHECKS
WHERE PAYROLCD in ('01', '02')
AND PYRLRTYP = 1
AND EMPLOYID = A.EMPLOYID
AND PYADNMBR = A.PYADNMBR