SQL显示从日期开始的旅行,其中司机少于雇用日期

时间:2015-06-10 21:46:13

标签: sql sql-server

学习,善待。

更有兴趣知道我是否正确地这样做...
我需要这个来显示运营商在2005年生效日期不到三年的旅行。这是解决这个问题的方法吗?

SELECT DISTINCT t.TripID, t.*
FROM Trips AS t, Runs AS r JOIN Operators AS o
ON r.OperatorID = o.OperatorID
WHERE t.EffectiveDate >= '01/01/2005' AND t.EffectiveDate <= '12/31/2005' 
AND o.HireDate <= DATEADD(YEAR, -3, '2005') AND t.ScheduleID IS NOT NULL

1 个答案:

答案 0 :(得分:1)

不确定,但我认为这就是你想要的:

SELECT
  t.TripID,
  t.RouteNumber,        
  t.StartLocation,      
  t.StartTime,          
  t.EndLocation,        
  t.EndTime,            
  t.EffectiveDate
FROM
  Trips AS t
  JOIN Schedules AS s 
    ON t.ScheduleID = s.ScheduleID
  JOIN Runs AS r
    ON s.RunID = r.RunID
  JOIN Operators AS o
    ON r.OperatorID = o.OperatorID
WHERE
  t.EffectiveDate BETWEEN '01/01/2005'  AND  '12/31/2005'
  AND DATEDIFF(MONTH, o.HireDate, t.EffectiveDate) < 36 -- Less than 3 years experience