问题
I'm not too familiar with the more complex queries and I'm having an issue finding some examples to take apart to utilize..
Event Name | Due Date | Frequency |Frequency UOM
S-XYC-001 | 10/17/2020 | 360 | D
S-XYA-003 | 6/1/2020 | 90 | D
S-XYC-004 | 4/3/2020 | 180 | D
So, we have a set of work that has an initial due date provided, and an expected frequency along with a unit of measurement (I've converted all my frequencies into days as there was previously a mix of days, quarterlies, months..).
I've been asked to generate a forecast of events in 2020. So, using the above source table we'd expect our list to look like this:
Event Name | Due Date | Frequency | Frequency UOM
S-XYC-001 | 10/17/2020 | 360 | D
S-XYA-003 | 6/1/2020 | 90 | D
S-XYA-003 | 8/30/2020 | 90 | D
S-XYA-003 | 11/28/2020 | 90 | D
S-XYC-004 | 4/3/2020 | 180 | D
S-XYC-004 | 9/30/2020 | 180 | D
I'm really not quite sure what type of query to apply to get these results, Or honestly what to look for to get some examples I can utilize to do what I need to.
回答1:
First, change your frequency to months - 12, 3, and 6 respectively - to obtain the correct result dates.
Then, create a small query and save it as Ten as it simply returns 0 to 9:
SELECT DISTINCT
Abs([id] Mod 10) AS N
FROM
MSysObjects;
Use that in this generic query that is capable of generating any sequence of date/time in the entire range of DateTime:
PARAMETERS
[Interval] Text ( 255 ),
[Number] IEEEDouble,
[Date] DateTime,
[Count] IEEEDouble;
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+[Ten_7].[N]*10000000 AS Id,
DateAdd([Interval],Fix([Number])*([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+[Ten_7].[N]*10000000),[Date]) AS [Date]
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,
Ten AS Ten_7
WHERE
((([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+[Ten_7].[N]*10000000)<[Count])
AND
(([Interval]) In ("s","n","h","d","w","ww","m","q","yyyy"))
AND
((Fix([Number])*([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+[Ten_7].[N]*10000000) Mod IIf(Fix([Number])=0,1,Fix([Number])))=0)
AND
((Ten_0.N)<=[Count]\1)
AND
((Ten_1.N)<=[Count]\10)
AND
((Ten_2.N)<=[Count]\100)
AND
((Ten_3.N)<=[Count]\1000)
AND
((Ten_4.N)<=[Count]\10000)
AND
((Ten_5.N)<=[Count]\100000)
AND
((Ten_6.N)<=[Count]\1000000)
AND
((Ten_7.N)<=[Count]\10000000));
Save it as DatesSequence.
This you can run with the parameters:
Interval: "m"
Number: 3, 6, or 12
Date: Due Date
Count: The count of future event dates to list
Results:
You could modify the query to filter out dates later than 2020.
来源:https://stackoverflow.com/questions/59494027/ms-access-query-to-generate-dates-for-recurring-events