Question : Need to update first booking of the day for each person within a time period

Hi peops, just wondering if you could help me out here. I've got this booking table and need to get, for each Carer, the first/earliest booking of their day, within a date range. The query below actually gets me the data as I need it, trouble is that I need the bookingID to be able to do anything with the data, and when I add the bookingID to the query, it also gives me bookings which are not the first ones in the Carer's day. How do I go about changing this select statement into an update statement? Or am I better off going about it a completely different way?

SELECT tmpmileagebookings.CarerID, Format([startdatetime],"yyyy-mm-dd") AS booking_day, Min(tmpmileagebookings.Startdatetime) AS first_booking
FROM tmpmileagebookings
GROUP BY tmpmileagebookings.CarerID, Format([startdatetime],"yyyy-mm-dd")
ORDER BY tmpmileagebookings.CarerID, Min(tmpmileagebookings.Startdatetime);

Answer : Need to update first booking of the day for each person within a time period

the Having clause from the previous post is not correct.  Plus, since the select statment and group by clauses include the booking ID, you will get a record for each booking ID.

Personally, I prefer one of two options.  The first option below should be updateable, the second is not.  However, the second runs significantly faster, especially with a large dataset.

SELECT tmpmileagebookings.CarerID,
             Format([startdatetime],"yyyy-mm-dd") AS booking_day,
             tmpmileagebookings.Startdatetime AS first_booking
FROM tmpmileagebookings
WHERE tmpMileageBookings.StartDateTime =
(SELECT MIn(T.StartDateTime)
 FROM tmpMileageBookings as T
 WHERE T.CarerID = tmpMileageBookings.CarerID
 AND Datevalue(T.StartDateTime) = DateValue(tmpMileageBookings.StartDateTime))
ORDER BY tmpmileagebookings.CarerID, tmpmileagebookings.Startdatetime

Another option is:

SELECT tmpmileagebookings.CarerID,
             Format([startdatetime],"yyyy-mm-dd") AS booking_day,
             tmpmileagebookings.Startdatetime AS first_booking
FROM tmpmileagebookings
INNER JOIN
(SELECT T.CarerID,
               DateValue(T.StartDateTime) as booking_Date,
               Min(T.StartDateTime) as first_booking
  FROM tmpMileageBookings as T
  GROUP BY T.CarerID, DateValue(T.StartDateTime)) as temp
ON tmpMileageBookings.CarerID = temp.CarerID
AND DateValue(tmpMileageBookings.StartDateTime) = temp.booking_Date
AND tmpmileagebookings.Startdatetime = temp.first_booking
ORDER BY tmpMileageBookings.CarerID, DateValue(tmpMileageBookings.StartDateTime)
Random Solutions  
 
programming4us programming4us