SQL Join to get most recent record

后端 未结 2 729
無奈伤痛
無奈伤痛 2020-12-20 04:13

I have three tables:

  • Measurements (MeasureID, Time, Distance, Value)
  • Events(EventID, Time Value)
  • EventValues
2条回答
  •  粉色の甜心
    2020-12-20 04:38

    You can use CROSS APPLY.

    SELECT  
        M.*, Data.* 
    FROM [Measure] M 
    CROSS APPLY
         (SELECT TOP 1 EV.* FROM [Event] E JOIN EventValues EV ON E.EventID = EV.EventID  
         WHERE M.Time >= E.Time ORDER BY E.Time DESC) AS Data
    ORDER BY M.Distance 
    

提交回复
热议问题