简体   繁体   中英

Working with avg, to_char, and having

Display the department ID, average salary (in currency format), and the number of job IDs for each department. Limit the results to only those departments with more than 5 job IDs.

What I've tried so far.

select department_id, to_char(avg(salary, '$999,999.00')), count(job_id) from employees group by department_id, salary, job_id having SUM(job_id) > '5';

I'm having trouble understand what it means by "invalid number of arguments."

在此处输入图片说明

Only department_id should be appearing in the GROUP BY clause since you want to aggregate over departments, and not anything else.

SELECT department_id,
       CONCAT('$', FORMAT(AVG(salary), 2)) AS avg_salary,
       COUNT(*) AS num_job_ids
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5

I assumed in my answer that every job_id in the table is unique. If this is not the case, and you wanted to instead count/report unique jobs, then you can replace COUNT(*) with COUNT(DISTINCT job_id) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM