2017-11-18 105 views
0

以下是工作存儲過程。如何將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  
+0

爲什麼你不能創建一個數據庫表來充當這些值的查找並使用'JOIN'?這是一大堆SQL。 – tadman

回答

0

這是您的重複表達:

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}部分:

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); 
+0

This works great !!!謝謝保羅。 – Vince

+0

嗨,這是我更新的存儲過程。有沒有辦法減少重複表達CASE'SI_CUM'和'SI_ANN'? – Vince