I’ve setup a table which contains records with multiple duplicated time fields (tblTime_Slots)
i.e.
ID Time_Slot
1 08:00
2 08:00
3 09:00
4 09:00
etc…
I want to bring back the Time_Slots in a combo box, but rather than bringing back each Time_slot twice I want to write an SQL query that will only display each Time_Slot once.
i.e.
ID Time_Slot
1 08:00
3 09:00
Therefore if the user allocates 08:00 to a booking then the next time the come to allocate a time to a new booking it returns the following in the combo box: -
ID Time_Slot
2 08:00
3 09:00
With the 08:00 Time_Slot now showing the record with ID 2 rather than ID 1 (which has been allocated to the previous booking).
If the time slot 08:00 with ID 2 was to then be allocated to a booking then the next time the query was run then the combo box wouldn’t show a 08:00 Time_Slot as they have all been allocated to previous bookings.
I’ve tried writing the following query: -
Expand|Select|Wrap|Line Numbers
- SELECT tbltime.ID, tbltime.Time
- FROM tbltime
- WHERE tbltime.Time in(SELECT DISTINCT tbltime.Time
- FROM tblTime
- GROUP BY tbltime.Time
- HAVING count(*) <=2);
Could the answer be to instead possibly return the MIN ID for the DISTINCT Time_Slots? Therefore for the 08:00 Time_Slot it would first of all return ID 1, then after that had been allocated to a booking it would return ID 2 for the 08:00 Time_Slot, then after that has been allocated to a booking it wouldn’t show a 08:00 Time_Slot.
Any help with this would be much appreciated as I’ve spent ages trying to figure it out. I appreciate that the query is in two parts, the first which displays only one of each of the repeated Time_Slot, and the next part of the query only returning Time_Slots that haven’t previously been allocated to a booking. I’m having that much trouble with the first part of the query I haven’t even got onto attempting the second part of the query.
Thanks