1

I am writing a query for my database and basically I want to query a database for events happening in each room. Meaning that i want to find the earliest event happening in room 1 and the earliest event happening in room 2.

Below I have the code for performing this query for 1 room. This code only returns the earliest event in room 2. How can I perform a query where I find the earliest event for room 1 and the earliest event for room 2 and return it in a single query?

This would be performing a query on each element of an array but in a single query. Is this possible or would I have to write a query for each room?

select   name,
         eventtime,
         MIN(eventtime - currenttime) as time_from_test
from     the_table
where    the_table.room = Room1
group by the_table.room
order by time_from_test
limit 1

Thanks in advance

1
  • ur group by columns r not matching with the select clause... Commented Mar 16, 2012 at 19:34

1 Answer 1

3
select   the_table.room,MIN(eventtime) as event_start_time_of_day
from     the_table
group by the_table.room;

For eg. if you have 5 rooms and each room has five event times then this query will retrieve the minimum event time and return it back to you by grouping all events at room level.

Sign up to request clarification or add additional context in comments.

3 Comments

To get more information (like the_table.name) you could join back to the_table again, I think.
Yup.. for getting other information I can join with the same table on room and get it...
Hi Venk, what if I am getting the rooms from another table? How would I find earliest events in the current table from the rooms that I select from the other table? Basically, I find the rooms in the other table and use the current table as a lookup table for events

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.