2017-09-04 75 views
0

考慮到我工作的企業中使用的商業日期,我需要更改我的數據庫的日期。將日期更改爲商業日期MyS

這裏的商業月份在YYYY-(M-1)-26YYYY-M-25之間。 其中M-1:上個月。

例如,今天的商業日期是2017-08-262017-09-25

但問題是在26至31(或30或28,一個月的最後一天)的範圍內,因爲在此範圍內,商業日期應爲YYYY-M-26YYYY-(M+1)-25,並且當月結束時再次使用YYYY-(M-1)-26YYYY-M-25

我爲此創建了一個查詢,但上述範圍中的問題無法修復。

select 
concat(
(  -- se mes tem 31 dias 
     if(month(current_date()) in (1,3,5,7,8,10,12), 
      -- então, diff(now() - data_i) <= 5? Se sim, incrementa +1 no mês, se não deixa -1 
      if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 5, date_format(concat(year(current_date()),'-',month(current_date())-0,'-',26),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())-1,'-',26),'%d/%m/%Y')), 
     -- se mes n tem 30 dias, ele tem 30 dias? 
     if(month(current_date()) in (4,6,9,11), 
      -- então, diff(now() - data_i) <= 4? Se sim, incrementa +1 no mês, se não deixa -1 
      if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 4, date_format(concat(year(current_date()),'-',month(current_date())-0,'-',26),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())-1,'-',26),'%d/%m/%Y')), 
     -- mes 29 
     if(month(current_date()) in (2), 
      -- então, diff(now() - data_i) <= 4? Se sim, incrementa +1 no mês, se não deixa -1 
      if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 3, date_format(concat(year(current_date()),'-',month(current_date())-0,'-',26),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())-1,'-',26),'%d/%m/%Y')), 
     999))) 
    ) 
,' a ', 
(  -- se mes tem 31 dias 
     if(month(current_date()) in (1,3,5,7,8,10,12), 
      -- então, diff(now() - data_i) <= 5? Se sim, incrementa +1 no mês, se não deixa -1 
      if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 5, date_format(concat(year(current_date()),'-',month(current_date())+1,'-',25),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())+0,'-',25),'%d/%m/%Y')), 
     -- se mes n tem 31 dias, ele tem 30 dias? 
     if(month(current_date()) in (4,6,9,11), 
      -- então, diff(now() - data_i) <= 4? Se sim, incrementa +1 no mês, se não deixa -1 
      if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 4, date_format(concat(year(current_date()),'-',month(current_date())+1,'-',25),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())+0,'-',25),'%d/%m/%Y')), 
     if(month(current_date()) in (2), 
      -- então, diff(now() - data_i) <= 4? Se sim, incrementa +1 no mês, se não deixa -1 
      if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 3, date_format(concat(year(current_date()),'-',month(current_date())+1,'-',25),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())+0,'-',25),'%d/%m/%Y')), 
     999))) 
    ) 
) as 'DataMesComercial' 
; 
+0

我鼓勵你來填充下面您的企業的業務日曆表規則。很明顯,您可以提高連接和條件下的性能。你需要幫助嗎? – Horaciux

+0

@Horaciux非常感謝。是的,但我在這裏建立一個數據倉庫,我的維度Date有超過3萬個寄存器。我在閏年,2月和7月/ 8月遇到了麻煩,創建了這張表。如果我手工創建這個表格,那麼問題就解決了,但我不會手動完成這個工作,那麼......如何用腳本來做到這一點?這裏應用的邏輯是什麼?如果我知道我知道查詢的邏輯。 –

+0

什麼日期設置你的期限?初始日期或結束日期?使其光滑。什麼專欄應該有所有真正的日期而沒有飛躍?開始階段還是結束階段? – Horaciux

回答

0

我想先簡單地解決一下這個問題,試着先用25號作爲起點,算出我們進入「商業月」的天數。從那裏,你可以決定,因爲你描述,是否去從M到M + 1或M-1至M.

例如:(在http://sqlfiddle.com/#!9/2228d7/41 SQL小提琴)

SET @date = DATE('2016-04-21'); 
SET @commercial_day = DAY(@date)-25; 
SET @commercial_month_start_offset = IF(@commercial_day<0, -1, 0); 
SET @commercial_month_start = DATE_ADD(@date, INTERVAL @commercial_month_start_offset MONTH); 
SET @commercial_month_end = DATE_ADD(@commercial_month_start, INTERVAL 1 MONTH); 
SET @commercial_date_start = DATE_ADD(DATE_ADD(MAKEDATE(YEAR(@commercial_month_start), 1), INTERVAL (MONTH(@commercial_month_start))-1 MONTH), INTERVAL (25)-1 DAY); 
SET @commercial_date_end = DATE_ADD(DATE_ADD(MAKEDATE(YEAR(@commercial_month_end), 1), INTERVAL (MONTH(@commercial_month_end))-1 MONTH), INTERVAL (26)-1 DAY); 
SELECT @date, @commercial_date_start,@commercial_date_end; 

我試着這與幾個不同的日期,如果我正確理解邏輯,相信這將適用於你。您可以將其轉換爲一個返回開始日期或結束日期的MySQL函數,或者如上例所示的字符串。

我也創造了這個與在SQL擺弄測試程序的功能:http://sqlfiddle.com/#!9/634312/1

這應該允許您驗證邏輯2017