我正在为我的Oracle课程提出以下问题。我有下面的解决方案,这有效,但我想格式化美元输出。当我添加“TO_CHAR”语句时,我收到“FROM”不在预期位置的错误。
家庭作业问题:
使用Oracle数据库employees表和CASE表达式解码部门ID。显示部门ID,姓氏,薪水和名为“New Salary”的列,其值基于以下条件: 如果部门ID是10那么1.25 *工资 如果部门ID是90那么1.5 *工资 如果部门ID是130那么1.75 *工资 否则,显示旧工资。
没有“TO_CHAR”的工作声明:
SELECT LAST_NAME AS "LAST NAME", DEPARTMENT_ID AS "DEPARTMENT ID", SALARY,
CASE DEPARTMENT_ID
WHEN 10 THEN SALARY * 1.25
WHEN 90 THEN SALARY * 1.50
WHEN 130 THEN SALARY * 1.75
ELSE SALARY END AS "NEW SALARY"
FROM EMPLOYEES;
声明错误:
SELECT LAST_NAME AS "LAST NAME, DEPARTMENT_ID AS "DEPARTMENT ID", SALARY,
CASE DEPARTMENT_ID
WHEN 10 THEN TO_CHAR (SALARY*1.25, '$99,999.99')
WHEN 90 THEN TO_CHAR (SALARY*1.50, '$99,999.99')
WHEN 130 THEN TO_CHAR (SALARY*1.75, '$99,999.99')
ELSE SALARY END AS "NEW SALARY"
FROM EMPLOYEES;
谢谢!
以下更正后的结果:
最后一个名称部门ID SALARY新SALARY King 90 24000 $ 36,000.00 Kochhar 90 17000 $ 25,500.00 De Haan 90 17000 $ 25,500.00
答案 0 :(得分:4)
Craig对这个问题的解释是正确的,但我不是只添加另一个TO_CHAR,而是将整个CASE表达式包装在一个TO_CHAR中:
SELECT LAST_NAME AS "LAST NAME", DEPARTMENT_ID AS "DEPARTMENT ID", SALARY,
TO_CHAR(
CASE DEPARTMENT_ID
WHEN 10 THEN SALARY * 1.25
WHEN 90 THEN SALARY * 1.50
WHEN 130 THEN SALARY * 1.75
ELSE SALARY END,
'$99,999.99'
) AS "NEW SALARY"
FROM EMPLOYEES;
答案 1 :(得分:3)
你不应该在ELSE声明中为工资制作一个to_char吗?
答案 2 :(得分:2)
问题是你的WHEN / THEN情况是返回一个字符串(因为你正在使用TO_CHAR),但你的ELSE正在返回一个数字。只需将TO_CHAR添加到ELSE即可。