以下是我创建Employee和Incentives表的架构。我想计算存储为时间戳的两个日期之间的数据差异。
CREATE TABLE Employee
("EMPLOYEE_ID" int, "FIRST_NAME" varchar2(9), "LAST_NAME" varchar2(7), "SALARY" int, "JOINING_DATE" timestamp, "DEPARTMENT" varchar2(9))
;
INSERT ALL
INTO Employee ("EMPLOYEE_ID", "FIRST_NAME", "LAST_NAME", "SALARY", "JOINING_DATE", "DEPARTMENT")
VALUES (1, 'John', 'Abraham', 1000000, '01-JAN-13 12.00.00 AM', 'Banking')
INTO Employee ("EMPLOYEE_ID", "FIRST_NAME", "LAST_NAME", "SALARY", "JOINING_DATE", "DEPARTMENT")
VALUES (2, 'Michael', 'Clarke', 800000, '01-JAN-13 12.00.00 AM', 'Insurance')
INTO Employee ("EMPLOYEE_ID", "FIRST_NAME", "LAST_NAME", "SALARY", "JOINING_DATE", "DEPARTMENT")
VALUES (3, 'Roy', 'Thomas', 700000, '01-FEB-13 12.00.00 AM', 'Banking')
INTO Employee ("EMPLOYEE_ID", "FIRST_NAME", "LAST_NAME", "SALARY", "JOINING_DATE", "DEPARTMENT")
VALUES (4, 'Tom', 'Jose', 600000, '01-FEB-13 12.00.00 AM', 'Insurance')
INTO Employee ("EMPLOYEE_ID", "FIRST_NAME", "LAST_NAME", "SALARY", "JOINING_DATE", "DEPARTMENT")
VALUES (5, 'Jerry', 'Pinto', 650000, '01-FEB-13 12.00.00 AM', 'Insurance')
INTO Employee ("EMPLOYEE_ID", "FIRST_NAME", "LAST_NAME", "SALARY", "JOINING_DATE", "DEPARTMENT")
VALUES (6, 'Philip', 'Mathew', 750000, '01-JAN-13 12.00.00 AM', 'Services')
INTO Employee ("EMPLOYEE_ID", "FIRST_NAME", "LAST_NAME", "SALARY", "JOINING_DATE", "DEPARTMENT")
VALUES (7, 'TestName1', '123', 650000, '01-JAN-13 12.00.00 AM', 'Services')
INTO Employee ("EMPLOYEE_ID", "FIRST_NAME", "LAST_NAME", "SALARY", "JOINING_DATE", "DEPARTMENT")
VALUES (8, 'TestName2', 'Lname%', 600000, '01-FEB-13 12.00.00 AM', 'Insurance')
SELECT * FROM dual
;
CREATE TABLE incentives
("EMPLOYEE_REF_ID" int, "INCENTIVE_DATE" timestamp, "INCENTIVE_AMOUNT" int)
;
INSERT ALL
INTO incentives ("EMPLOYEE_REF_ID", "INCENTIVE_DATE", "INCENTIVE_AMOUNT")
VALUES (1, '01-Feb-2013 12:00:00 AM', 5000)
INTO incentives ("EMPLOYEE_REF_ID", "INCENTIVE_DATE", "INCENTIVE_AMOUNT")
VALUES (2, '01-Feb-2013 12:00:00 AM', 3000)
INTO incentives ("EMPLOYEE_REF_ID", "INCENTIVE_DATE", "INCENTIVE_AMOUNT")
VALUES (3, '01-Feb-2013 12:00:00 AM', 4000)
INTO incentives ("EMPLOYEE_REF_ID", "INCENTIVE_DATE", "INCENTIVE_AMOUNT")
VALUES (1, '01-Jan-2013 12:00:00 AM', 4500)
INTO incentives ("EMPLOYEE_REF_ID", "INCENTIVE_DATE", "INCENTIVE_AMOUNT")
VALUES (2, '01-Jan-2013 12:00:00 AM', 3500)
SELECT * FROM dual
;
当执行下面的选择时,我得到的是对象对象而不是差异,这是正确的语法吗?
查询:
Select FIRST_NAME,INCENTIVE_DATE - JOINING_DATE from
employee a inner join incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
输出:
| FIRST_NAME | INCENTIVE_DATE-JOINING_DATE |
|------------|-----------------------------|
| John | [object Object] |
| John | [object Object] |
| Michael | [object Object] |
| Michael | [object Object] |
| Roy | [object Object] |
编辑:
使用此在线平台sqlfiddle.com/#!4/aa339/15作为oracle 11g R2
答案与问题37 http://a4academics.com/interview-questions/53-database-and-sql/397-top-100-database-sql-interview-questions-and-answers-examples-queries?showall=&start=2的以下网站上给出的答案相同。如果对象对象表示什么?
答案 0 :(得分:1)
尝试使用TRUNC()
SELECT
FIRST_NAME
, TRUNC(INCENTIVE_DATE) - TRUNC(JOINING_DATE)
FROM employee a
INNER JOIN incentives B ON A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
减去2个时间戳很痛苦,因为它会产生一系列不同的单位值