使用Oracle

时间:2017-02-20 10:16:15

标签: sql oracle

ID  Date        NAME    START_TIME              END_TIME            
1   2/15/2017   A       2/15/20173:40:39 PM     2/15/2017 3:41:17 PM
2   2/15/2017   B       2/15/20173:40:39 PM     2/15/2017 3:41:17 PM
3   2/15/2017   C       2/15/20173:40:39 PM     2/15/2017 3:41:17 PM    

我遇到了一个问题,我需要从2016年1月到今天用这3个语句填充我的数据库。 我可以尝试的一个解决方案是我可以编写只需循环的java代码并为表创建一个新日期和新条目,然后我可以使用生成的查询进行插入。

但有什么办法可以用oracle做到这一点。

2 个答案:

答案 0 :(得分:1)

这是生成给定开始日期和结束日期的日期的常用方法,您只需加入名称列表即可获得所需内容:

with names as (
    select 'A' as name from dual union all
    select 'B' as name from dual union all
    select 'C' as name from dual 
),
dates as (
            select date' 2017-02-18' + level -1 as yourDate,
            level as lev
            from dual
            connect by date' 2017-02-18' + level -1 <= date '2017-02-20')
select rownum, name, yourDate, lev
from names
       cross join dates

必须对其进行轻微编辑,以更好地适应列的数量和类型。它是如何工作的一个小例子:

    ROWNUM N YOURDATE         LEV
---------- - --------- ----------
         1 A 18-FEB-17          1
         2 B 18-FEB-17          1
         3 C 18-FEB-17          1
         4 A 19-FEB-17          2
         5 B 19-FEB-17          2
         6 C 19-FEB-17          2
         7 A 20-FEB-17          3
         8 B 20-FEB-17          3
         9 C 20-FEB-17          3

给出:

  chart: {
    events: {
      load: function() {
        const menu = document.createElement('div');
        menu.style.cssText = 'left:' + (this.plotLeft + this.plotWidth * 0.5) + 'px; top:' + (this.plotTop + this.plotHeight * 0.1) + 'px; position: absolute; display: none; background-color:white; padding: 20px';

        let str = '';

        this.series[0].data.forEach(point => {
          str += '<p><div style="display:inline-block;padding-right: 10px;width:10px;height:10px;background-color:' + point.color + ';background-clip:content-box;"></div>' + point.name + '</p>'
        });

        str += '';
        menu.innerHTML = str;
        this.renderTo.appendChild(menu);

        this.subtitle.on('mouseenter', function(e) {
          menu.style.display = 'block';
        })
      }
    }
  },

答案 1 :(得分:0)

作为一个基本概念,像这样的东西会这样做......你需要适应A,B和C,或者重复每一个。

with Numbers (NN) as
(
select 1 as NN
from dual
union all
select NN+1
from Numbers
where NN <2000
)
insert into MyTable (ID, Date, Name, StartTime, EndTime)
select NN + 3,  -- If repeating, replace the 3 with the max(id) after each run
       'A', 
       to_date('20170215','YYYYMMDD') - NN, 
       to_date('20170215 154039','YYYYMMDD HH24MISS') - NN,
       to_date('20170215 154117','YYYYMMDD HH24MISS') - NN
from NN
where NN <= 365