473,411 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

How to limit DateDiff to a calendar year when data ranges extend beyond that year?

269 256MB
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? :-)
Mar 7 '20 #1
8 3307
cactusdata
214 Expert 128KB
This can be solved using some generic queries.

First, create this tiny query and save it as Ten:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT Abs([id] Mod 10) AS N
  2. FROM MSysObjects;
Next, this query to generate days of months (within the entire range of Date) and save it as MonthsDateRange:

Expand|Select|Wrap|Line Numbers
  1. PARAMETERS 
  2.     [DateStart] DateTime, 
  3.     [DateEnd] DateTime;
  4. SELECT 
  5.     [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, 
  6.     [DateStart] AS DateStart, 
  7.     [DateEnd] AS DateEnd, 
  8.     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
  9. FROM 
  10.     Ten AS Ten_0, 
  11.     Ten AS Ten_1, 
  12.     Ten AS Ten_2, 
  13.     Ten AS Ten_3, 
  14.     Ten AS Ten_4, 
  15.     Ten AS Ten_5, 
  16.     Ten AS Ten_6
  17. WHERE 
  18.     (((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])) 
  19.     AND 
  20.     ((Ten_0.N)<=DateDiff("m",[DateStart],[DateEnd])\1) 
  21.     AND 
  22.     ((Ten_1.N)<=DateDiff("m",[DateStart],[DateEnd])\10) 
  23.     AND 
  24.     ((Ten_2.N)<=DateDiff("m",[DateStart],[DateEnd])\100) 
  25.     AND 
  26.     ((Ten_3.N)<=DateDiff("m",[DateStart],[DateEnd])\1000) 
  27.     AND 
  28.     ((Ten_4.N)<=DateDiff("m",[DateStart],[DateEnd])\10000) 
  29.     AND 
  30.     ((Ten_5.N)<=DateDiff("m",[DateStart],[DateEnd])\100000) 
  31.     AND 
  32.     ((Ten_6.N)<=DateDiff("m",[DateStart],[DateEnd])\1000000));
  33.  
Finally, this query, DaysMonthsDateRange, will return the day count for each (partial) month:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     MonthsDateRange.Id, 
  3.     MonthsDateRange.DateStart, 
  4.     MonthsDateRange.DateEnd, 
  5.     Year([DateMonth]) AS [Year], 
  6.     Month([DateMonth]) AS [Month], 
  7.     IIf(DateDiff("m",[DateStart],[DateMonth])=0,[DateStart],DateSerial(Year([DateMonth]),Month([DateMonth]),1)) AS DateFrom, 
  8.     IIf(DateDiff("m",[DateEnd],[DateMonth])=0,[DateEnd],DateSerial(Year([DateMonth]),Month([DateMonth])+1,1)) AS DateTo, 
  9.     DateDiff("d",[DateFrom],[DateTo]) AS Days
  10. FROM 
  11.     MonthsDateRange;
Result:

Mar 7 '20 #2
DanicaDear
269 256MB
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.
Mar 7 '20 #3
DanicaDear
269 256MB
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.)
Attached Images
File Type: jpg OccupancyRate.jpg (43.4 KB, 39 views)
Mar 7 '20 #4
cactusdata
214 Expert 128KB
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:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     ID, 
  3.     Condo, 
  4.     Arrival AS DateStart, 
  5.     Departure AS DateEnd
  6. FROM 
  7.     Occupancy;
Next, this fits a default query of mine, DaysInMonthsOfDateRanges, a Cartesian (multiplying) query, here modified to include field Condo:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT 
  2.     DateRanges.Id, 
  3.     DateRanges.Condo,
  4.     DateRanges.DateStart, 
  5.     DateRanges.DateEnd, 
  6.     10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10) AS Factor, 
  7.     Year(DateAdd("m",[Factor],[DateStart])) AS [Year], 
  8.     Month(DateAdd("m",[Factor],[DateStart])) AS [Month], 
  9.     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, 
  10.     DateDiff("d",[DateStart],[DateEnd]) AS DaysTotal
  11. FROM 
  12.     MSysObjects AS Uno, 
  13.     MSysObjects AS Deca, 
  14.     DateRanges
  15. WHERE 
  16.     (((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:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     Id, 
  3.     Condo, 
  4.     DateStart As Arrival, 
  5.     DateEnd As Departure, 
  6.     DaysTotal As Nights
  7. FROM 
  8.     DaysInMonthsOfDateRanges
  9. GROUP BY 
  10.     Id, 
  11.     Condo, 
  12.     DateStart, 
  13.     DateEnd, 
  14.     DaysTotal
  15. ORDER BY 
  16.     DateStart;
Output:

Mar 7 '20 #5
Rabbit
12,516 Expert Mod 8TB
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.
Mar 9 '20 #6
NeoPa
32,556 Expert Mod 16PB
Hi Danica (My dear).

This may help some (Time Interval Overlapping (MS Access 2003)).

Do let us know if you've managed to get a solution for this :-)
Apr 25 '20 #7
DanicaDear
269 256MB
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.
Apr 27 '20 #8
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.
Apr 27 '20 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
3
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...
1
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...
6
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...
30
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...
3
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...
6
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...
4
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...
10
RockKandee
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...
1
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...
0
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.