如何使用MAX Date选择查询?

时间:2014-06-23 11:31:39

标签: sql

我正在尝试从Database获取最新日期。但是在'Reservation.EntryDate'

上收到查询错误
Select Rooms.RoomTitle,Rooms.IsReserved,Reservation.CheckInTime,Reservation.CheckOutTime
From Reservation,Rooms 
INNER JOIN
(SELECT Reservation.RoomID, MAX(Reservation.EntryDate) AS MaxDateTime
    FROM Reservation
    GROUP BY Reservation.RoomID) groupedtt 
ON Rooms.RoomID = groupedtt.RoomID 
AND Reservation.EntryDate = groupedtt.MaxDateTime

我做错了什么?

1 个答案:

答案 0 :(得分:1)

不要混合隐式和显式连接语法

Select ro.RoomTitle, 
       ro.IsReserved, 
       res.CheckInTime, 
       res.CheckOutTime
From Reservation res
join Rooms ro ON ro.RoomID = res.RoomID
join
(
    SELECT RoomID, MAX(EntryDate) AS MaxDateTime
    FROM Reservation
    GROUP BY RoomID
) groupedtt ON ro.RoomID = groupedtt.RoomID 
           AND res.EntryDate = groupedtt.MaxDateTime