How do I Automatically insert monthly records into a table via SQL?

自闭症网瘾萝莉.ら 提交于 2019-12-11 18:18:17

问题


I'm trying to generate monthly records in one table based on instructions in another table. Software - MS Access 2007, though I'm looking for an SQL solution here. To greatly simplify the matter, let's say the following describes the tables:

 TaskManager:
  - DayDue
  - TaskName

 Task:
  - DateDue
  - TaskName

So what happens is that there may be an entry in TaskManager {15, "Accounts due"}, so this should lead to an "Account due" record in the Task table with the due date being the 15th of each month. I'd want it to create records for the last few months and the next year.

What I'm thinking that I need to do is first create a SELECT query that results in x records for each record in the TaskManager table, with a date for each month. After that, I do an INSERT query which inserts records into the Task table if they do not EXIST in the aforementioned SELECT query.

I think I can manage the INSERT query, though I'm having trouble figuring out how to do the SELECT query. Could someone give me a pointer?


回答1:


You could use a calendar table.

INSERT INTO Task ( DateDue, TaskName )
SELECT calendar.CalDate, TaskManager.TaskName
FROM calendar, TaskManager
WHERE (((Day([CalDate]))=TaskManager.DayDue) 
AND ((calendar.CalDate)<#7/1/2013#));

The calendar table would simply contain all dates and other such relevant fields as work day (yesno). Calendar tables are generally quite useful.




回答2:


Here is the solution I developed using Remou's Calendar table idea.

First create a Calendar table, which simply contains all dates for a desired range. It's easy to just make the dates in Excel and paste them into the table. This is also a very reliable way of doing it, as Excel handles leap years correctly for the modern range of dates.

After building this table, there are three queries to run. The first is a SELECT, which selects every possible task generated by the TaskManager based on the date and frequency. This query is called TaskManagerQryAllOptions, and has the following code:

SELECT TaskManager.ID, Calendar.CalendarDate
FROM TaskManager INNER JOIN Calendar ON
        TaskManager.DateDay = Day(Calendar.CalendarDate)
WHERE (TaskManager.Frequency = "Monthly")
    OR (TaskManager.Frequency = "Yearly" AND
            TaskManager.DateMonth = Month(Calendar.CalendarDate))
    OR (TaskManager.Frequency = "Quarterly" AND
            (((Month(Calendar.CalendarDate)- TaskManager.DateMonth) Mod 3) =  0));

The bulk of the above is to cover the different options a quarterly Day and Month pair could cover. The next step is another SELECT query, which selects records from the TaskManagerQryAllOptions in which the date is within the required range. This query is called TaskManagerQrySelect.

SELECT TaskManagerQryAllOptions.ID, TaskManager.TaskName,
        TaskManagerQryAllOptions.CalendarDate
FROM TaskManagerQryAllOptions INNER JOIN TaskManager
        ON TaskManagerQryAllOptions.ID = TaskManager.ID
WHERE (TaskManagerQryAllOptions.CalendarDate > Date()-60)
    AND (TaskManagerQryAllOptions.CalendarDate < Date()+370)
    AND (TaskManagerQryAllOptions.CalendarDate >= TaskManager.Start)
    AND ((TaskManagerQryAllOptions.CalendarDate <= TaskManager.Finish) 
            OR (TaskManager.Finish Is Null))
ORDER BY TaskManagerQryAllOptions.CalendarDate;

The final query is an INSERT. As we will be using this query frequently, we don't want it to generate duplicates, so we need to filter out already created records.

INSERT INTO Task ( TaskName, TaskDate )
SELECT TaskManagerQrySelect.TaskName, TaskManagerQrySelect.CalendarDate
FROM TaskManagerQrySelect
WHERE Not Exists(
    SELECT *
    FROM Task
    WHERE Task.TaskName = TaskManagerQrySelect.TaskName
        AND Task.TaskDate = TaskManagerQrySelect.CalendarDate);

One limitation of this method is that if the date of repetition (e.g. the 15th of each month) is changed, the future records with the wrong day will remain. A solution to this would be to update all the future records with the adjusted date, then run the insert.




回答3:


One possibility could be to create a table of Months, and a table of Years (prior year, current, and next one). I could run a SELECT query which takes the Day from the TaskManager table, the Month from the Month table, and the Year from the Year table - I imagine that this could somehow create my desired multiple records for a single TaskManager record. Though I'm not sure what the exact SQL would be.



来源:https://stackoverflow.com/questions/12263492/how-do-i-automatically-insert-monthly-records-into-a-table-via-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!