如何把a表里的数据自动插入到b表里?
a表里有字段atime字段和name字段
a表是动态数据。
如:a表里记录
atime                     name
2009-06-30 01:00:00       老a
2009-06-30 01:10:00       老b
2009-06-30 01:20:00       老c
2009-06-30 02:00:00       老d
2009-06-30 21:00:00       老e
2009-07-01 00:00:00       甲
2009-07-01 01:00:00       乙
2009-07-01 23:00:00       丁
b表里的字段是btime字段和count字段
如:b表里的记录
btime           count
2009-06-30       5
2009-07-01       3
如何做到,每当在第二天凌晨00:00:00时,oracle会自动把前一天的数据统计出来插入到b表里。注:只统计前一天的。

解决方案 »

  1.   

      路过学习。
         通过程序可以解决。
         但不知在oracle里怎么解决
      

  2.   

    trigger
    或者
    用job调度存储过程
      

  3.   

    create materialized view b
    REFRESH
    START WITH TRUNC(SYSDATE) next TRUNC(SYSDATE)+1
    as
    select deptno, count(*) from emp group by deptno;
      

  4.   

    用job吧,每天早上执行一次就行了
      

  5.   


     create materialized view b on prebuilt table
     REFRESH
     START WITH TRUNC(SYSDATE+1) next TRUNC(SYSDATE)+1
     as
     select trunc(a.atime) btime, count(a.name) count
      from a
     where a.atime>= trunc(sysdate-1) 
       and a.atime<  trunc(sysdate)
     group by trunc(a.atime);我走口猜着改了下,不知是否可行!仅供参考
      

  6.   

    有两种实现方式:
    1、通过oracle的job程序实现,通过定时调用存储过程
    2、通过主机操作系统的crontab命令,定时调用shell,这个需要shell和sql结合使用,相对来说难度稍微大点
      

  7.   

    drop table a;
    create table a(atime date,name varchar2(20));
    drop table b;
    create table b(btime date,count int);create or replace trigger t_a_idu_a
    after insert or delete or update on a
    begin
      delete b;
      insert into b select trunc(atime),count(name) from a group by trunc(atime);
    end;insert into a values(to_date('2009-05-31 02:00:00','yyyy-mm-dd hh24:mi:ss'),'老a');
      

  8.   

    用触发器是实时的,如果每天0点计算,可以写个存储过程,用job定时去执行.
    variable job number; 
    begin 
      sys.dbms_job.submit(job => :job, 
                          what => Pro_test;', 
                          next_date => trunc(sysdate), 
                          interval => 'trunc(sysdate)+1'); 
      commit; 
    end; 
      

  9.   

    /data1/aiobs7wq/zhengzw/SHELLSQL>% sqlSQL*Plus: Release 9.2.0.7.0 - Production on 星期五 7月 31 11:31:00 2009Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.
    Connected to:
    Oracle9i Enterprise Edition Release 9.2.0.7.0 - 64bit Production
    With the Partitioning, OLAP and Oracle Data Mining options
    JServer Release 9.2.0.7.0 - ProductionSQL> create table a(atime date,name varchar2(20)); Table created.SQL> create table b(btime date,count int); Table created.SQL> insert into a values(to_date('2009-06-30 01:00:00','yyyy-mm-dd hh24:mi:ss'),'老a'); 1 row created.SQL> insert into a values(to_date('2009-06-30 01:10:00','yyyy-mm-dd hh24:mi:ss'),'老b'); 1 row created.SQL> insert into a values(to_date('2009-06-30 01:20:00','yyyy-mm-dd hh24:mi:ss'),'老c'); 1 row created.SQL> insert into a values(to_date('2009-06-30 02:00:00','yyyy-mm-dd hh24:mi:ss'),'老d'); 1 row created.SQL> insert into a values(to_date('2009-06-30 21:00:00','yyyy-mm-dd hh24:mi:ss'),'老e'); 1 row created.SQL> insert into a values(to_date('2009-07-01 00:00:00','yyyy-mm-dd hh24:mi:ss'),'甲'); 1 row created.SQL> insert into a values(to_date('2009-07-01 01:00:00','yyyy-mm-dd hh24:mi:ss'),'乙'); 1 row created.SQL> insert into a values(to_date('2009-07-01 23:00:00','yyyy-mm-dd hh24:mi:ss'),'丁');1 row created.SQL> select * from a;ATIME               NAME
    ------------------- --------------------
    2009-06-30 01:00:00 老a
    2009-06-30 01:10:00 老b
    2009-06-30 01:20:00 老c
    2009-06-30 02:00:00 老d
    2009-06-30 21:00:00 老e
    2009-07-01 00:00:00 甲
    2009-07-01 01:00:00 乙
    2009-07-01 23:00:00 丁8 rows selected.SQL> 而涉及到的shell程序为:/data1/aiobs7wq/zhengzw/SHELLSQL>% cat test.sh
    #!/bin/bash
    sqlplus -S /nolog > result.log <<! 
    set heading off feedback off pagesize 0 verify off echo off
    conn obs61p11/obs61p11@aiobs6
    insert into b select to_date(substr(to_char(atime,'yyyy-mm-dd hh24:mi:ss'),1,10),'yyyy-mm-dd'),count(*) from a group by to_date(substr(to_char(atime,'yyyy-mm-dd hh24:mi:ss'),1,10),'yyyy-mm-dd');
    commit;
    exit
    EOF
    !在当前目录下执行:sh test.sh即可写的sql语句是整理全部的,不是前一天,LZ稍微改一下就可以了