扩展日期并插入到另一个表格中。

huangapple go评论54阅读模式
英文:

Expand the date and insert into another table

问题

需要扩展表格的第二行部分,并插入到名为fct的表格中。
表格1:- 部分
表格2:- fct
不依赖于A列,而B列在查询中将以类似于'%MT'的形式出现。

部分
    
    a      b        c       d        e        f        g
    
    600  MT_000    6765  29-04-22  30-04-22  1    WEEK
         MT        6758  01-05-22  31-05-22  1    MONTH
    601  MT_0001   6771  01-06-22  30-06-22  1    WEEK
    
    
    
想要的样子

FCT

 a       b        c       d         e          f        g   
  600  MT_000    6765  30-04-22  30-04-22  1    WEEK
       MT        6758  01-05-22  07-05-22  1    MONTH
       MT        6758  08-05-22  14-05-22  1    MONTH   
       MT        6758  15-05-22  21-05-22  1    MONTH   
       MT        6758  22-05-22  28-05-22  1    MONTH   
       MT        6758  29-05-22  31-05-22  1    MONTH   
       MT        6758  01-06-22  02-06-22  1    WEEK  
英文:

Need to expand this second row of table part
and insert into table fct table.
Table 1:- Part
Table 2:- fct
do not depend on A column and B column will in a form in query is b like '%MT'

 Part 
    
    a		b		    c			d 			e       f     g		
    
    600	  MT_000		6765	29-04-22	30-04-22	1	WEEK
     	  MT			6758	01-05-22	31-05-22	1	MONTH
    601	  MT_0001		6771	01-06-22	30-06-22	1	WEEK
    
    


want like this 

FCT

 a		b		        c       d 	         e              f       g	
  600      MT_000		        6765	30-04-22	30-04-22	1	WEEK
	       MT			6758	01-05-22	07-05-22	1	MONTH
		MT			6758	08-05-22	14-05-22	1	MONTH	   
		MT			6758	15-05-22	21-05-22	1	MONTH	   
		MT			6758	22-05-22	28-05-22	1	MONTH	   
		MT			6758	29-05-22	31-05-22	1	MONTH	
		MT			6758	01-06-22	02-06-22	1	WEEK	

答案1

得分: 0

你可以像这样应用递归子查询来生成以月份为单位的行:

select a, b, c, case g when 'MONTH' then d1 else d end d, least(d2, e) e, f, g
from fct
cross apply (
  select trunc(d, 'mm') + (level - 1) * 7 d1, trunc(d, 'mm') + level * 7 - 1 d2
  from dual 
  connect by g = 'MONTH' and trunc(d, 'mm') + (level - 1) * 7 <= e ) 

dbfiddle

英文:

You can apply recursive subquery producing weeks to month-type rows like here:

select a, b, c, case g when &#39;MONTH&#39; then d1 else d end d, least(d2, e) e, f, g
from fct
cross apply (
  select trunc(d, &#39;mm&#39;) + (level - 1) * 7 d1, trunc(d, &#39;mm&#39;) + level * 7 - 1 d2
  from dual 
  connect by g = &#39;MONTH&#39; and trunc(d, &#39;mm&#39;) + (level - 1) * 7 &lt;= e ) 

dbfiddle

huangapple
  • 本文由 发表于 2023年5月10日 22:22:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定