简体   繁体   中英

math and to_char

Display branch name, number of employees as Headcount, total Salary as Payroll, and average salary as "Average Salary" for branches that have more than one employee. Format all dollar amounts with the "$" format.

Below is my code to answer this query however I am receiving an invalid number error.

select br_branchname, count (st_staffno) as "Headcount", 
    sum(to_char (st_salary, '$99,999.00')) as "Payroll", 
    avg(to_char (st_salary, '$99,999.00')) as "Average Salary"
from branch join staff
on st_br_branchno = br_branchno
group by br_branchname
having count (st_staffno) > 1;

You've got TO_CHAR and AVG/SUM reversed:

select br_branchname,
       count (st_staffno) as "Headcount", 
       TO_CHAR(SUM(st_salary), '$99,999.00') as "Payroll", 
       TO_CHAR(AVG(st_salary), '$99,999.00') as "Average Salary"
from branch
INNER join staff
  on st_br_branchno = br_branchno
group by br_branchname
having count (st_staffno) > 1

You need to have TO_CHAR on the "outside" of the stacked function calls - otherwise you're attempting to compute the average and sum of a character string containing a '$' , and Oracle quite rightly pitches a fit about $ not being a valid digit.

Best of luck.

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