Domanda

1.) List the revenue for each hotel received so far i.e. for DepartureDate < NOW(). The calculation must be done in the SQL statement. Determine the length of each reservation (i.e. number of days) using the DateDiff function and multiply this value by the room rate (not the discounted rate). Include the hotel number in the output.

So far I have tried this but it's not right and giving me aggregate function error for DateDiff statement

SELECT ROOM.HotelNo, DateDiff("d", [ArrivalDate], [DepartureDate]) *  ROOM_TYPE.RoomRate AS TotalRevenue
FROM RESERVATION, ROOM_TYPE, ROOM
WHERE ROOM.RoomType = ROOM_TYPE.RoomType
AND RESERVATION.RoomNo = ROOM.RoomNo
AND DepartureDate > Now()
GROUP BY ROOM.HotelNo;

Here is the link where you can see the Table

RelationShip Table <-- LINK

Please help me to solve this problem

È stato utile?

Soluzione

Use Sum() of the DateDiff() expression to avoid the "does not include the specified expression ... as part of an aggregate function" complaint.

SELECT
    ROOM.HotelNo,
    Sum(
        DateDiff(
                "d",
                [ArrivalDate],
                [DepartureDate]
            ) * ROOM_TYPE.RoomRate
        ) AS TotalRevenue

The WHERE clause includes DepartureDate > Now(), which I suspect filters the results to those which have not yet departed. I think you want < instead of > there.

Consider using INNER JOIN instead of using the WHERE clause to specify how rows from the tables are matched. If you can build this query from Design View of the Access query designer, setting up the joins will be easy. And the query designer knows the rule about required parentheses when joining more than 2 tables ... so will produce SQL which keeps the db engine happy.

Altri suggerimenti

As engineersmnky points out, your DATEDIFF is wrong. It should be 2 parameters, and departure then arrival date:

SELECT 
    ROOM.HotelNo, 
    DATEDIFF(RESERVATION.DepartureDate, RESERVATION.ArrivalDate) *  ROOM_TYPE.RoomRate AS TotalRevenue
FROM RESERVATION
JOIN ROOM ON 
    RESERVATION.RoomNo = ROOM.RoomNo
JOIN ROOM_TYPE ON
    ROOM.RoomType = ROOM_TYPE.RoomType
WHERE RESERVATION.DepartureDate > NOW()
GROUP BY ROOM.HotelNo;

I am not sure why you have brackets around the DepartureDate and ArrivalDate as they are just columns on the RESERVATION table correct?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top