[英]Calculate column date value from variable and other column date offset
我有一個包含 1000 個任務/記錄的項目管理表,每個任務/記錄都有一個動態的starting_date
和ending_date
列。 這些日期的值是通過采用 static 項目“上線”日期(例如2022-10-01
)然后減去每條記錄的task_offset_days
值(例如 -211),得到starting_date
值來計算的。 結束日期是通過days_to_complete
列計算的,例如60
天的值,如果將其添加到starting_date
就會得到最終的ending_date
。
示例:Live date = 2022-10-01
,偏移-211
天導致starting_date = '2022-03-04'
,加上days_offset
60
導致ending_date = 2022-05-03
。
這里的問題:
我想將 1000 條記錄復制到另一個表,並在此過程中想根據另一個上線日期更新starting_date
和ending_date
值。 考慮到我正在復制和編輯 1000 條記錄,我怎樣才能以最有效的方式做到這一點?
當前副本查詢:
@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`;
它可能比你想象的要簡單。 您只需將值傳遞到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.