I want to calculate an occupancy rate for nights stayed in a hotel. The occupancy rate needs to be for a calendar year.
The fields I am working with are
Arrival
Departure
I have parameters set up in the query as criteria on the Departure field.
FromDate
ToDate
Most of the arrival dates AND departure dates fall within the year...but the reservation occupying the hotel room on January 1 and on December 31 do not start/stop on those exact dates.
For example, someone stayed from December 25 through January 5...I only want 4 of those nights in that year's count (January 1, 2, 3, 4--checkout is on the 5th, they didn't stay that night). My query would count 11.
This is what I'm running in my query, which is beautiful except for the first and last reservation of the year.
Nights: DateDiff("d",[Arrival],[Departure])
parameters Between [FromDate] and [ToDate] which I have set as criteria on the Departure field (and that would be Jan 1 and Dec 31 of any year).
I know how to get what I want...but I don't know how to write it in the query or in code (OMG VBA---I'm just not there. LOL!)
If the Arrival is before (less than?) FromDate, then I need to calculate that entry Between FromDate and Departure (not between Arrival and Departure).
ALSO
If the Departure is after (greater than?) ToDate, then I need to calculate that entry Between Arrival and ToDate (not between Arrival and Departure).
All the other entries are good.
Who out there is smarter than me? :-)
8 3307
This can be solved using some generic queries.
First, create this tiny query and save it as Ten: - SELECT DISTINCT Abs([id] Mod 10) AS N
-
FROM MSysObjects;
Next, this query to generate days of months (within the entire range of Date) and save it as MonthsDateRange: - PARAMETERS
-
[DateStart] DateTime,
-
[DateEnd] DateTime;
-
SELECT
-
[Ten_0].[N]+[Ten_1].[N]*10+[Ten_2].[N]*100+[Ten_3].[N]*1000+[Ten_4].[N]*10000+[Ten_5].[N]*100000+[Ten_6].[N]*1000000 AS Id,
-
[DateStart] AS DateStart,
-
[DateEnd] AS DateEnd,
-
DateAdd("m",[Ten_0].[N]+[Ten_1].[N]*10+[Ten_2].[N]*100+[Ten_3].[N]*1000+[Ten_4].[N]*10000+[Ten_5].[N]*100000+[Ten_6].[N]*1000000,[DateStart]) AS DateMonth
-
FROM
-
Ten AS Ten_0,
-
Ten AS Ten_1,
-
Ten AS Ten_2,
-
Ten AS Ten_3,
-
Ten AS Ten_4,
-
Ten AS Ten_5,
-
Ten AS Ten_6
-
WHERE
-
(((DateAdd("m",[Ten_0].[N]+[Ten_1].[N]*10+[Ten_2].[N]*100+[Ten_3].[N]*1000+[Ten_4].[N]*10000+[Ten_5].[N]*100000+[Ten_6].[N]*1000000,[DateStart]))<=DateAdd("m",DateDiff("m",[DateStart],DateAdd("d",-1,[DateEnd])),[DateStart]))
-
AND
-
((Ten_0.N)<=DateDiff("m",[DateStart],[DateEnd])\1)
-
AND
-
((Ten_1.N)<=DateDiff("m",[DateStart],[DateEnd])\10)
-
AND
-
((Ten_2.N)<=DateDiff("m",[DateStart],[DateEnd])\100)
-
AND
-
((Ten_3.N)<=DateDiff("m",[DateStart],[DateEnd])\1000)
-
AND
-
((Ten_4.N)<=DateDiff("m",[DateStart],[DateEnd])\10000)
-
AND
-
((Ten_5.N)<=DateDiff("m",[DateStart],[DateEnd])\100000)
-
AND
-
((Ten_6.N)<=DateDiff("m",[DateStart],[DateEnd])\1000000));
-
Finally, this query, DaysMonthsDateRange, will return the day count for each (partial) month: - SELECT
-
MonthsDateRange.Id,
-
MonthsDateRange.DateStart,
-
MonthsDateRange.DateEnd,
-
Year([DateMonth]) AS [Year],
-
Month([DateMonth]) AS [Month],
-
IIf(DateDiff("m",[DateStart],[DateMonth])=0,[DateStart],DateSerial(Year([DateMonth]),Month([DateMonth]),1)) AS DateFrom,
-
IIf(DateDiff("m",[DateEnd],[DateMonth])=0,[DateEnd],DateSerial(Year([DateMonth]),Month([DateMonth])+1,1)) AS DateTo,
-
DateDiff("d",[DateFrom],[DateTo]) AS Days
-
FROM
-
MonthsDateRange;
Result:
Holy cow! That was WAY MORE difficult than I anticipated. Thank you so much for your detailed response. I will study it.
What are the "4" days in your result on line 2? I see what the 7 days are. But I'm not following the 4.
So I saved all the queries as you mentioned but how do I get MY existing query talking to these? I'm pretty confused because I use the field "Arrival" and then I use "FromDate" as a parameter to limit the time the query looks at. I'm not sure if DateStart and DateFrom are supposed to be replaced with Arrival/Departure or if those are new fields just used to make your queries run.
I'm stuck! (Ugh. So sorry.)
That can be done in a similar way.
First, create a tiny query to alias your field names those of mine in the next query (I'm lazy) and save it as DateRanges: - SELECT
-
ID,
-
Condo,
-
Arrival AS DateStart,
-
Departure AS DateEnd
-
FROM
-
Occupancy;
Next, this fits a default query of mine, DaysInMonthsOfDateRanges, a Cartesian (multiplying) query, here modified to include field Condo: - SELECT DISTINCT
-
DateRanges.Id,
-
DateRanges.Condo,
-
DateRanges.DateStart,
-
DateRanges.DateEnd,
-
10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10) AS Factor,
-
Year(DateAdd("m",[Factor],[DateStart])) AS [Year],
-
Month(DateAdd("m",[Factor],[DateStart])) AS [Month],
-
IIf(DateDiff("m",[DateStart],[DateEnd]) = 0,DateDiff("d",[DateStart],[DateEnd]),IIf(DateDiff("m",DateAdd("m",[Factor],[DateStart]),[DateEnd])=0,Day([DateEnd])-1,Day(DateSerial(Year(DateAdd("m",[Factor],[DateStart])),Month(DateAdd("m",[Factor],[DateStart]))+1,0))-IIf([Factor] = 0,Day([DateStart])-1,0))) AS Days,
-
DateDiff("d",[DateStart],[DateEnd]) AS DaysTotal
-
FROM
-
MSysObjects AS Uno,
-
MSysObjects AS Deca,
-
DateRanges
-
WHERE
-
(((10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10)) <= DateDiff("m",[DateStart],[DateEnd])));
Output:
Finally, create an aggregating query to finish it up: - SELECT
-
Id,
-
Condo,
-
DateStart As Arrival,
-
DateEnd As Departure,
-
DaysTotal As Nights
-
FROM
-
DaysInMonthsOfDateRanges
-
GROUP BY
-
Id,
-
Condo,
-
DateStart,
-
DateEnd,
-
DaysTotal
-
ORDER BY
-
DateStart;
Output:
An alternative method that avoids the dummy queries is to join the two tables on the overlapping date ranges. Then you can just calculate datediff on the larger of the 2 start dates to the smaller of the 2 end dates.
On a related note, you said your criteria was on the departure date. But make sure that's what you actually want because that will miss any record that falls within the report date range but ends outside the report date range.
NeoPa! I'm so glad to hear from you again!! I hope you are doing well. I had given up on this but will study it again perhaps. Thank you for the additional reference.
Sometimes I get in over my head. It doesn't take much, really. haha.
I'll post back if/when I get this worked out. I think the solutions here have been excellent...I just gave up to soon.
NeoPa 32,556
Expert Mod 16PB
The solutions have been posted by two very clever people. There's no question about that.
Just so you know, I'm still happy to receive a call from you if ever you find you're stuck with something. It's been a very long time since we actually spoke. Send me a message if you'd like to.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: MT |
last post by:
hi all,
I have a view that has data from this year as well as previous years.
the select statment looks something like this:
create view as select
year,
costs_mon1,
costs_mon2,
costs_mon3...
|
by: Shmulik |
last post by:
I have an application written in C# that creates a queue of items to
process, connects via a TCP connection to a server on a Unix box, and
then passes data files to the Unix box for processing...
|
by: marc.henderson |
last post by:
Hi all,
I want to add a month callendar to my application that allows data
entry and will display data. Basically I would like the user to select
a day and then enter data for that day. When...
|
by: Burghew |
last post by:
Hello,
I generate invoices for my customers evry month. I want to keep a form which
will allow the user to select the Month and Year through a combo and thus
generate reports based on the month...
|
by: Charles Law |
last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this
once before, but ...".
Two users are both looking at the same data, from a database. One user
changes the data and...
|
by: jay.meerdink |
last post by:
Greetings!
I have a simple Windows form for editing. Search results are bound to
a bindingsource and shown in a datagrid paired with a datanavigator.
The current row's data is displayed in text...
|
by: sirnickettynox |
last post by:
I have a list of club members ranging in age from 5 years to 62 years of age. They are grouped into several age groups ie. under 9's, under 12's, under 15's, under 17's, under 20's, and Seniors. The...
|
by: neelsfer |
last post by:
I use the recordset method to add data to a form called frmchipxc and the table involved is called RaceEntry2. When data is added here, i want to run another function in a subform called...
|
by: RockKandee |
last post by:
Hi,
I am getting an error message when trying to print.
I am using the MS access calendar found here:
http://bytes.com/topic/access/answers/761255-ms-access-calendar
I have Windows 8 and...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |