如何仅根据日期

时间:2017-05-19 00:27:46

标签: sql-server date

我需要让我的查询只返回基于员工开始日期的最新记录。我想我需要一个最大日期内连接,但我不能让它工作。有没有人对我有任何建议?我正在使用TOAD for SQL Developer。

SELECT
  e.No_,
  ep.[Classification Description],
  ep.[Employee Starting Date],
  ep.[Standard Rate],
  ep.[Over Award Rate],
  ep.[Employee Rate],
  p.[Job Title],
  e.[First Name],
  e.[Last Name],
  e.[Payroll No_]
FROM Employee e,
     [Payroll Employee] p,
     [Employee Pay Rate] ep
WHERE e.No_ = p.[Employee No_]
AND e.No_ = ep.[Employee No_]
AND ep.[Employee No_] = p.[Employee No_]
AND e.[Payroll No_] = 'PAY7'
AND CAST(e.[Termination Date] AS date) = '1753-01-01'

1 个答案:

答案 0 :(得分:0)

您可以进行分组,并获得相应的最小或最大日期,如下所示:

SELECT
  e.No_,
  ep.[Classification Description],
  MAX(ep.[Employee Starting Date]) as [Employee Starting Date], --hope it is min based on your logic
  ep.[Standard Rate],
  ep.[Over Award Rate],
  ep.[Employee Rate],
  p.[Job Title],
  e.[First Name],
  e.[Last Name],
  e.[Payroll No_]
FROM Employee e
    INNER JOIN [Payroll Employee] p
    ON e.No_ = p.[Employee No_]
    INNER JOIN [Employee Pay Rate] ep
    ON e.No_ = ep.[Employee No_]
    AND ep.[Employee No_] = p.[Employee No_]
WHERE 
    e.[Payroll No_] = 'PAY7'
AND CAST(e.[Termination Date] AS date) = '1753-01-01'
GROUP BY 
  e.No_,
  ep.[Classification Description],
  ep.[Standard Rate],
  ep.[Over Award Rate],
  ep.[Employee Rate],
  p.[Job Title],
  e.[First Name],
  e.[Last Name],
  e.[Payroll No_]