SQL Group by Query with aggregate function as well as detail information (1 Viewer)

Herosoft

New member
Local time
Today, 18:12
Joined
Oct 6, 2012
Messages
5
Given a simple table with historical info about Hotelroom occupancies such as name of the guest, date in, type of room, number of nights, ...
I want to make a query resulting in 1 output record that shows the highest number of nights someone has ever stayed in the hotel, as well as the corresponding detail information: who was this customer and which type of room was he in?
Using the 'group by' and 'max' function it's easy to solve the first question, but I am not able to show the corresponding detailed information in that record (person's name, type of room).
I've done some experiments with a nested query in the 'expression' field, also trying a selfjoin but always getting errors. I'm confident there must be a solution? Can anyone help me out? Thank you!
 

Guus2005

AWF VIP
Local time
Today, 18:12
Joined
Jun 26, 2007
Messages
2,641
Sometimes you need to split the problem in more queries. One to retrieve the highest number of nights a second to gather the detailed information based on the first query and sometimes even a third query is needed.
Once you have it running, you can try to combine all the queries into a single one as long as Access doesn't think the query is too complex.

For future reference however, having more queries it is easier to debug and understand.

HTH:D
 

Herosoft

New member
Local time
Today, 18:12
Joined
Oct 6, 2012
Messages
5
I have been thinking in that direction (see my original post), but it seems I cannot get it working. I'm trying to figure out how this working query:
SELECT Max(HOTEL.NumberOfNights) AS MaxOfNumberOfNights
FROM HOTEL;
can be used as a criteria in a second query where I select name and roomtype from the record containing the maximum number of nights
SELECT HOTEL.NameGuest, HOTEL.RoomCategory
FROM HOTEL;
I'm using the design grid of the query and used a totals query with the 'where' or the 'expression' option on the 'group by' line but I keep receiving syntax errors.
 

Users who are viewing this thread

Top Bottom