不,这不对。将为您提供员工的SUM(INV_PRICE)
总销售额,但AVG(INV_PRICE)
将为您提供每位员工的平均销售额,而不是他们总销售额的平均值(对于所有员工)。
试试这个,你想要为所有员工计算总销售额的平均值——即使是那些根本没有销售额的员工:
SELECT EMP_ID, SUM(INV_PRICE) AS TOTAL_SALES
FROM INVOICE
GROUP BY EMP_ID
HAVING SUM(INV_PRICE) <=
( SELECT SUM(INV_PRICE) FROM INVOICE )
/ ( SELECT COUNT(*) FROM EMPLOYEE )
;
或者如果你想要所有有销售的员工的平均值:
SELECT EMP_ID, SUM(INV_PRICE) AS TOTAL_SALES
FROM INVOICE
GROUP BY EMP_ID
HAVING SUM(INV_PRICE) <=
( SELECT SUM(INV_PRICE) / COUNT(DISTINCT EMP_ID)
FROM INVOICE )
;
跳过那个,Access 没有COUNT(DISTINCT )
:
SELECT EMP_ID, SUM(INV_PRICE) AS TOTAL_SALES
FROM INVOICE
GROUP BY EMP_ID
HAVING SUM(INV_PRICE) <=
( SELECT AVG(TOTAL_SALES)
FROM
( SELECT SUM(INV_PRICE) AS TOTAL_SALES
FROM INVOICE
GROUP BY EMP_ID
) tmp
) ;