如何制定这个SQL查询?

时间:2010-11-03 20:55:23

标签: sql sql-server

我希望employee numbersstart dates的所有员工start dates等于最早的日期。

我知道这是错的。但只是写作来展示我想要的东西。

SELECT     start_date, employee_no
FROM         [employees]
WHERE     (start_date = MIN(start_date))

4 个答案:

答案 0 :(得分:7)

你很亲密!

SELECT     start_date, employee_no
FROM         [employees]
WHERE     start_date = (SELECT MIN(start_date) FROM employees)

答案 1 :(得分:3)

我会使用通用表达式表(CTE)来执行此操作。这就像创建一个临时表。

;with EmpInfo as 
(
    SELECT start_date, employee_no, MIN(start_date) OVER () as MinStartDate
    FROM [employees]
)
SELECT start_date, employee_no FROM EmpInfo WHERE start_date = MinStartDate

这是关于Using Common Table Expressions

的Microsoft网页

答案 2 :(得分:0)

这样做:

SELECT     start_date, employee_no 
FROM         [employees] 
WHERE     start_date = (select MIN(start_date) from [employees])

答案 3 :(得分:0)

SELECT    start_date, employee_no
FROM      [employees]
WHERE     start_date = (SELECT MIN(start_date) FROM [employees])