简体   繁体   English

从变量和其他列日期偏移计算列日期值

[英]Calculate column date value from variable and other column date offset

I have a project management table with 1000 tasks/records, each with a dynamic starting_date and ending_date column.我有一个包含 1000 个任务/记录的项目管理表,每个任务/记录都有一个动态的starting_dateending_date列。 The values of those dates are calculated by taking a static project "going-live" date (eg 2022-10-01 ) and then deducting the task_offset_days value of each record (eg -211), resulting in the starting_date value.这些日期的值是通过采用 static 项目“上线”日期(例如2022-10-01 )然后减去每条记录的task_offset_days值(例如 -211),得到starting_date值来计算的。 The end date is calculated via a days_to_complete column with eg 60 days value, which if added to the starting_date gives me the final ending_date .结束日期是通过days_to_complete列计算的,例如60天的值,如果将其添加到starting_date就会得到最终的ending_date

Example: Live date = 2022-10-01 , offset by -211 days results in starting_date = '2022-03-04' , which plus days_offset of 60 results in ending_date = 2022-05-03 .示例:Live date = 2022-10-01 ,偏移-211天导致starting_date = '2022-03-04' ,加上days_offset 60导致ending_date = 2022-05-03

Here the issue:这里的问题:

I want to copy the 1000 records to another table and during the process want to update the starting_date , ending_date value based on another going-live date.我想将 1000 条记录复制到另一个表,并在此过程中想根据另一个上线日期更新starting_dateending_date值。 How can I do this in the most efficient way considering I am copying and editing 1000 records?考虑到我正在复制和编辑 1000 条记录,我怎样才能以最有效的方式做到这一点?

Current copy query:当前副本查询:

@live_date = '2022-10-01';   // going-live date

INSERT INTO `new_task_table` (
    `property_id`,  // unique identifier, not present in original table
    `status`,
    `task_desc`,
    `phase`,
    `workstream`,
    `task_level`,
    `task_owner_group`,
    `start_date`,   // = live date - offset
    `end_date`,     // = start date + days to complete
    `offset_days`,  // eg. -211 (note: can be below / above zero: -211 or 120)
    `days_to_complete`, // eg. 60
    `created_at`
)
SELECT 
    '31',
    `status`,
    `task_desc`,
    `phase`,
    `workstream`,
    `task_level`,
    `task_owner_group`,
    `start_date`,
    `end_date`,
    `offset_days`,
    `days_to_complete`,
    '2022-01-01 00:00:00'   // date of query execution
FROM `old_task_table`;

It's probably simpler than you think.它可能比你想象的要简单。 You just pass the values into the SELECT您只需将值传递到SELECT

@live_date = '2022-10-01';   // going-live date

INSERT INTO `new_task_table` (
    `property_id`,  // unique identifier, not present in original table
    `status`,
    `task_desc`,
    `phase`,
    `workstream`,
    `task_level`,
    `task_owner_group`,
    `start_date`,   // = live date - offset
    `end_date`,     // = start date + days to complete
    `offset_days`,  // eg. -211 (note: can be below / above zero: -211 or 120)
    `days_to_complete`, // eg. 60
    `created_at`
)
SELECT 
    '31',
    `status`,
    `task_desc`,
    `phase`,
    `workstream`,
    `task_level`,
    `task_owner_group`,
    DATE_ADD(@live_date, INTERVAL offset_days DAY) AS start_date,
    DATE_ADD(@live_date,INTERVAL offset_days + days_to_complete DAY) AS end_date,
    `offset_days`,
    `days_to_complete`,
    '2022-01-01 00:00:00'   // date of query execution
FROM `old_task_table`;

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

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