SQL where子句用于命名表

时间:2013-02-05 18:05:58

标签: sql oracle11g

当我没有年龄字段进行检查时,如何使用where子句检查下面的年龄显示?我正在使用oracle DB。

select LNAME || ', ' || FNAME as "Name", 
       TO_CHAR(sysdate,'YYYY') - TO_CHAR(DOB,'YYYY') as AGE 
  from STAFF
 where AGE = (select min(TO_CHAR(sysdate,'YYYY') - TO_CHAR(DOB,'YYYY')) 
                from STAFF);

2 个答案:

答案 0 :(得分:1)

选择最年轻的员工:

select  *
from    (
        select  *
        from    STAFF
        order by
                DOB desc
        ) SubQueryAlias
where   rownum = 1

Example at SQL Fiddle.

答案 1 :(得分:0)

以下是现有Oracle表的一些示例:

SELECT * FROM 
(
 SELECT ename as "Name"
      , TO_CHAR(sysdate,'YYYY') - TO_CHAR(hiredate,'YYYY') as AGE 
  FROM scott.emp
)
WHERE age = (SELECT min(TO_CHAR(sysdate,'YYYY') - TO_CHAR(hiredate,'YYYY')) FROM scott.emp)
/

Name    AGE
----------------
SCOTT   26
ADAMS   26

您还可以按年龄或任何其他字段添加分析函数以进行分区,或者只按年龄顺序排序,以查看输出中的所有emps删除...