0

请帮助以下查询显示所有员工的员工姓名、部门名称、薪水、等级、经验(例如:XX 年 YY 月 ZZ 天)

ENAME     DNAME    SAL  GRADE      EXPERIENCE

SCOTT    RESEARCH  3000   4        12 Years 3 months 15 days

像这样我需要得到输出。

我试图写长达数年但数月,我无法获得的日子。

select distinct ename,dname,sal,grade,
 (round((months_between(sysdate,hiredate)/12))||' years' EXP 
 from emp,salgrade,dept 
 where dept.deptno=emp.deptno and sal between losal and highsal;
4

3 回答 3

1

你有岁月。

用于MONTHS_BETWEEN(date1, date2)获取月份。然后减去(年 * 12)。

用于DAYS_BETWEEN(date1, date2)获取天数。

有关更多详细信息,请参阅此

于 2012-10-19T07:12:14.617 回答
0

这是我正在使用的 T-SQL (SQL Server) 解决方案(基于迄今为止的测试)。可悲的是,我没有可以玩的 oracle 实例,但希望这不应该太难转换:

declare @fromDate date = '2010-11-21'
, @toDate date = getutcdate()

--from date must be before to date
declare @tempDate date
if @toDate < @fromDate 
begin
    set @tempDate = @toDate
    set @toDate = @fromDate
    set @fromDate = @tempDate
end

declare @fDD int = datepart(dd,@fromdate)
, @tDD int = datepart(dd,@todate)
, @fMM int = datepart(mm,@fromdate)
, @tMM int = datepart(mm,@todate)
, @fYYYY int = datepart(yyyy,@fromdate)
, @tYYYY int = datepart(yyyy,@todate)
, @y int, @m int, @d int

--calc difference in years
set @y = @tYYYY-@fyyyy
if @fMM > @tMM or (@fMM=@tMM and @fDD > @tDD)
begin
    set @y = @y - 1
    set @fYYYY = @fYYYY + @y
    set @tempDate = DATEADD(year,@y,@fromDate)
end

--calc remaining difference in months
set @m = DATEDIFF(month,@tempDate,@toDate)
if @tDD < @fDD 
begin
    set @m = @m-1
    set @tempDate = DATEADD(month,@m,@tempDate)
end

--calc remaining difference in days
set @d = DATEDIFF(day,@tempDate,@toDate)

--display results in user friendly and grammatically correct way
select cast(@y as nvarchar(3)) + N' year' + case when @y = 1 then N'' else N's' end + N' '
+ cast(@m as nvarchar(2)) + N' month' + case when @m = 1 then N'' else N's' end + N' '
+  cast(@d as nvarchar(3)) + N' day' + case when @d = 1 then N'' else N's' end + N' '
于 2012-10-19T08:23:26.087 回答
0

这是您在 ORACLE SQL 中所需的答案

select (floor(months_between(sysdate,hiredate)/12)) YEARS,
round(((months_between(sysdate,hiredate)/12)-(round(months_between(sysdate,hiredate)/12)))*12) months
from abc;
于 2013-12-15T10:34:27.100 回答