select to_char(hiredate,'MM') from (
select deptno, MAX(sal) from emp
group by deptno having deptno = 10
);
select to_char(hiredate, 'MM') as hire_month
from emp
where deptno = 10;
select to_char(hiredate, 'YYYY-MM') as hire_month, max(sal) as max_sal
from emp
where deptno = 10
group by to_char(hiredate, 'YYYY-MM')
order by hire_month;
select to_char(e.hiredate, 'MM') as hire_month
from emp e
join (
select max(sal) as max_sal from emp where deptno = 10
) m on e.sal = m.max_sal and e.deptno = 10;
select hd, max(sal)
from (
select to_char(hiredate,'yyyymm') hd, sal
from emp
where deptno=10
)
group by hd;
此寫法將「按入職年月分組,求每組最高薪資」,邏輯清晰,符合 SQL 標準。
to_char(date, 'fmt') 格式化字串區分大小寫,'MM'=月,'MON'=月縮寫,'YYYY'=四位年。EXPLAIN PLAN 查看執行計劃,優化性能。