2017-09-04 72 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