简体   繁体   English

选择查询,日期差异与MS Access上的多个记录

[英]Select query, difference in dates with multiple record on MS Access

I am working on a database, but having trouble with something(total noob). 我正在使用数据库,但遇到了某些问题(总计菜鸟)。 I can't post the images yet on this account, I have posted links to my images. 我无法在该帐户上发布图像,我已经发布了指向图像的链接。 Basically, I have a reservation table and a room table. 基本上,我有一个预订表和一个房间表。 I wanted to make a Date Diff operation For each room, and multiply it by rent price, and group it by room number. 我想对每个房间进行Date Diff操作,然后将其乘以租金,然后按房间号分组。 The end result I was trying to make is like this, after I hide the date difference because I would only need it for the calculation. 在隐藏日期差之后,我试图做出的最终结果是这样的,因为我只需要它来进行计算。

 <!DOCTYPE html> <html> <body> <table style="width:100%"> <tr> <th>Room Number</th> <th>Total</th> </tr> <tr> <td>1</td> <td>375</td> </tr> <tr> <td>2</td> <td>375</td> </tr> <tr> <td>3</td> <td>1680</td> </tr> <tr> <td>4</td> <td>700</td> </tr> <tr> <td>6</td> <td>60</td> </tr> <tr> <td>7</td> <td>540</td> </tr> </table> </body> </html> 

I have this as my SQL Query: 我将其作为我的SQL查询:

SELECT [Room Number], 
DATEDIFF("d", min(CheckInDate), max(CheckOutDate)) as dif
FROM Reservations
GROUP by [Room Number]

I did not do the calculation for money part yet, as I cant even make the Datediff work for each room. 我还没有进行货币部分的计算,因为我什至无法使Datediff适用于每个房间。

在此处输入图片说明

I think you need to calculate the datediff for each room-customer pair and then group by room_id. 我认为您需要为每个房间-客户对计算datediff,然后按room_id进行分组。 You can multiple the price afterwords or during the group by. 您可以在价格后标或分组依据期间添加多个价格。 I don't know ms-access but by sql logic it should look like: 我不知道ms-access,但是通过sql逻辑它应该看起来像:

SELECT [Room Number], SUM(DATEDIFF("d", CheckInDate, CheckOutDate)) * price_per_night as dif 
FROM Reservations 
GROUP by [Room Number]

Let's assume your bookings are stored in a table called Reservations and the room info is stored in a table called Rooms. 假设您的预订存储在名为Reservations的表中,而房间信息存储在名为Rooms的表中。 If you want to include all rooms, whether they were booked or not, use this: 如果要包括所有房间,无论是否已预订,请使用以下方法:

SELECT Rooms.[Room Number], Sum((Reservations.CheckOutDate-Reservations.CheckInDate)*Rooms.Price) AS [Total per room]
FROM Reservations RIGHT JOIN Rooms ON Reservations.[Room Number] = Rooms.[Room Number]
GROUP BY Rooms.[Room Number];

Replace RIGHT JOIN by INNER JOIN if you're only interested in rooms that were booked. 如果您只对已预订的房间感兴趣,请用INNER JOIN代替RIGHT JOIN。

PS your HTML example is wrong for room 7, it should be 1080 (9*120). PS您的HTML示例对于7号会议室是错误的,应该为1080(9 * 120)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM