带有舍入的日期操作给出错误结果(db.grussell.org上的oracle sql)
我正在db.grussell.org上练习execises中的SQL,教程5()中的练习10询问员工的月年龄 问题是带有舍入的日期操作给出错误结果(db.grussell.org上的oracle sql),sql,oracle,Sql,Oracle,我正在db.grussell.org上练习execises中的SQL,教程5()中的练习10询问员工的月年龄 问题是 How old is each employee in months. Format this as employee number against age in months. Round to the nearest whole number of months. 这是我的代码(empno是员工ID,dob是员工表上的日期类型的出生日期 select e.empno,
How old is each employee in months.
Format this as employee number against age in months.
Round to the nearest whole number of months.
这是我的代码(empno是员工ID,dob是员工表上的日期类型的出生日期
select e.empno,
round( months_between ( trunc(sysdate,'mm'),trunc(dob) ) )
from employee e
这是我的结果
EMPLOYEE MONTHS_BETWEEN
1 882
2 845
3 674
4 647
5 705
6 756
7 832
8 714
9 714
10 670
11 658
12 700
13 902
14 853
15 748
16 658
17 683
18 673
19 702
20 640
21 622
22 927
23 751
24 810
25 758
26 689
27 779
28 732
29 586
30 633
31 744
32 695
对于员工编号为5、10和16的行,由于我无法理解的原因,结果是不正确的,对于所有其他行,结果是正确的
我怀疑我在round函数中遗漏了一些东西,或者没有在参数之间的月份上加上或减去一些日期
有没有人能给我指出正确的方向,告诉我这些会有什么问题?
我真的不明白为什么有些数据是正确的,有些是不正确的。。。
有可能这个练习是错误的,但我无法判断。原因是您正在截断
sysdate
。因此,从本月(2017年7月)开始,您将测量7月1日的年龄,而不是当前日期
相反,只要做:
select e.empno,
round(months_between(sysdate, e.dob)) as age_in_months
from employee e;
谢谢Gordon,我在使用上一个练习中的truncate,所以copy+past将它们放在那里,我没有意识到这会破坏操作。