数学和to_char

时间:2019-04-23 02:09:05

标签: sql oracle

对于拥有多个员工的分支机构,显示分支机构名称,员工人数(员工人数),工资总额(工资)和平均工资水平(“平均工资”)。用“ $”格式设置所有美元金额。

下面是我的代码来回答此查询,但是我收到无效的数字错误。

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;

1 个答案:

答案 0 :(得分:2)

您将TO_CHARAVG/SUM颠倒了:

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

您需要在堆叠函数调用的“外部”使用TO_CHAR-否则,您将试图计算包含'$'的字符串的平均值和总和,而Oracle则是正确的确定$不是有效数字的合适位置。

好运。