来自关键字错误的地方

问题描述 投票:0回答:2

显示房地产经纪人员工号,姓名,年薪为年薪,月薪为月薪,年龄为年龄。将月薪提高到小数点后2位。圆形年龄。按员工年龄下降排序输出。如果记录中没有生日,则将年龄列为“未知”。这是我必须回答这个问题的代码。

select st_staffno, st_name, st_salary "Annual Salary",(st_salary/12) as "Monthly Salary"   
    decode (st_birthdate, null, 'unknown'), 
    round sysdate ((st.birthdate)/365.25,0) as "Age"
from staff
order by "Age" desc;

它返回的是未找到预期错误的From关键字。

sql oracle
2个回答
2
投票

您应该使用您正在使用的DBMS标记SQL问题。从DECODE我得出结论它是Oracle。当查询解析器认为ORA-00923 "FROM keyword not found where expected"子句已完成,但后面没有SELECT关键字时,您会得到FROM。那么什么可能使DBMS认为SELECT条款已经结束?当您错过所选表达式之间的逗号时,通常会发生这种情况。

你的错误:

  1. as "Monthly Salary"之后有一个逗号丢失。
  2. 您的年龄计算在语法上是关闭的。
  3. 您正在使用st.birthdate,但查询中没有st表名或别名。我想列名是st_birthdate

更正的查询:

select
  st_staffno,
  st_name,
  st_salary as "Annual Salary",
  st_salary / 12 as "Monthly Salary"   
  decode(st_birthdate, null, 'unknown'), 
  round((sysdate - st_birthdate) / 365.25, 0) as "Age"
from staff
order by "Age" desc;

您也可以使用标准SQL的DECODE而不是CASE WHEN。并且从字面上应用“然后将年龄列为未知”,你必须结合最后两个表达式。你错过了“将月薪提高到2位小数”。

select
  st_staffno,
  st_name,
  st_salary as "Annual Salary",
  round(st_salary / 12, 2) as "Monthly Salary"   
  case when st_birthdate is null
       then 'unknown'
       else to_char(round((sysdate - st_birthdate) / 365.25, 0))
  end as "Age"
from staff
order by st_birthdate nulls last;

最后:我们通常不会从一年的实际长度来计算年龄。出生在4月1日,我们在4月1日午夜时分变老了一年。

extract(year from sysdate) - extract(year from st_birthdate) -
case when to_char(sysdate, 'mmdd') < to_char(st_birthdate, 'mmdd')
  then 1 else 0 
end as "Age"

0
投票

你需要一个直接的支架(a轮,因为圆形是一个功能

.. round(sysdate/362.25,0)并检查你的逻辑评估Age列它应该是一些解码条件,如..decode(birthdate,round(sysdate..ontrue),null (onfalse)

© www.soinside.com 2019 - 2024. All rights reserved.