gpt4 book ai didi

mysql - 将重复语句分配给变量

转载 作者:行者123 更新时间:2023-11-29 18:11:29 24 4
gpt4 key购买 nike

下面是工作存储过程。如何将 case 语句下的重复代码分配给变量并使用它,以便减少编码行数?该过程的目的是使用每个 CASE 语句的 start_date 和 end_date 更新表。感谢您的帮助和建议。

BEGIN
UPDATE period x
SET start_date = CASE
when return_period = 'L3M' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -2 Month))
when return_period = 'YTD' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL 1-MONTH((select max(monthend_date) from returns y
WHERE x.account_id=y.account_id)) Month))
when return_period = '1YR_CUM' or return_period = '1YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -11 Month))
when return_period = '2YR_CUM' or return_period = '2YR_ANN' then
last_day(date_add((select max(monthend_date) from eturns y WHERE x.account_id=y.account_id),INTERVAL -23 Month))
when return_period = '3YR_CUM' or return_period = '3YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -35 Month))
when return_period = '4YR_CUM' or return_period = '4YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -47 Month))
when return_period = '5YR_CUM' or return_period = '5YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -59 Month))
when return_period = '6YR_CUM' or return_period = '6YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -71 Month))
when return_period = '7YR_CUM' or return_period = '7YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -83 Month))
when return_period = '8YR_CUM' or return_period = '8YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -95 Month))
when return_period = '9YR_CUM' or return_period = '9YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -107 Month))
when return_period = '10YR_CUM' or return_period = '10YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -119 Month))
when return_period = '11YR_CUM' or return_period = '11YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -131 Month))
when return_period = '12YR_CUM' or return_period = '12YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -143 Month))
when return_period = '13YR_CUM' or return_period = '13YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -155 Month))
when return_period = '14YR_CUM' or return_period = '14YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -167 Month))
when return_period = '15YR_CUM' or return_period = '15YR_ANN' then
last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL -179 Month))
when return_period = 'SI_CUM' or return_period = 'SI_ANN' then
(select min(monthend_date) from returns y WHERE x.account_id=y.account_id)
END
,
end_date=(select max(monthend_date) from returns y WHERE x.account_id=y.account_id)
;
END

最佳答案

这是您的重复表达式:

last_day(date_add((select max(monthend_date) from returns y WHERE x.account_id=y.account_id),INTERVAL {months} MONTH))

唯一的区别是 {months} 中的值。首先,我将表达式写得短一些:

last_day((select max(monthend_date) from returns y WHERE x.account_id=y.account_id) + INTERVAL {months} MONTH)

然后我将仅对 {month} 部分使用 CASE:

UPDATE period x
SET start_date = last_day((select max(monthend_date) from returns y WHERE x.account_id=y.account_id) + INTERVAL
CASE
when return_period = 'L3M' then -2
when return_period = 'YTD' then -1
when return_period = '1YR_CUM' or return_period = '1YR_ANN' then -11
when return_period = '2YR_CUM' or return_period = '2YR_ANN' then -23
when return_period = '3YR_CUM' or return_period = '3YR_ANN' then -35
when return_period = '4YR_CUM' or return_period = '4YR_ANN' then -47
# [...]
END
MONTH),
end_date=(select max(monthend_date) from returns y WHERE x.account_id=y.account_id);

你也可以这样写:

UPDATE period x
SET start_date = last_day((select max(monthend_date) from returns y WHERE x.account_id=y.account_id) + INTERVAL
CASE return_period
when 'L3M' then -2
when 'YTD' then -1
when '1YR_CUM' then -11
when '1YR_ANN' then -11
when '2YR_CUM' then -23
when '2YR_ANN' then -23
when '3YR_CUM' then -35
when '3YR_ANN' then -35
when '4YR_CUM' then -47
when '4YR_ANN' then -47
# [...]
END
MONTH),
end_date=(select max(monthend_date) from returns y WHERE x.account_id=y.account_id);

关于mysql - 将重复语句分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47362027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com