0

我有一个员工表,其中的字段是:

first_name, last_name, hire_date, salary, department_id, department_name, 等等。

我打算找出EMPLOYEE1 and EMPLOYEE2、 那么EMPLOYEE2 and EMPLOYEE3、 等等 之间的雇佣日期差异。

我必须在 sql 中编写一个查询来显示员工的名字和雇用日期差异

4

3 回答 3

1

要在 Microsoft SQL 2012 中使用天(用年、小时等代替天)来查找日期之间的差异:

Select datediff(day, HireDate, EndDate) 
From Employee1
于 2014-10-24T13:20:01.353 回答
1

Since you've still not mentioned what RDBMS you are using i'll start with SQL-Server:

WITH x 
     AS (SELECT first_name, 
                last_name, 
                hire_date, 
                salary, 
                department_id, 
                department_name, 
                hireNum=Row_number() 
                          OVER( 
                            ORDER BY hire_date) 
         FROM   dbo.employee) 
SELECT DiffDays=Datediff(day, x.hire_date, x2.hire_date),
       first_name, 
       last_name, 
       hire_date, 
       salary, 
       department_id, 
       department_name 
FROM   x 
       INNER JOIN x x2 
               ON x.hirenum = x2.hirenum + 1 
于 2013-01-26T15:18:55.090 回答
1

我们可以使用DATEDIFF来计算日期差异。例如

SELECT DATEDIFF(SELECT DATE_ADD(start_date,INTERVAL 1 DAY),end_date);

希望对你有帮助

也有使用to_days函数的方法。点击这里了解更多详情

于 2013-01-26T15:07:05.957 回答