Search code examples
sqlmysqloracle-xe

How to automatically backup a ratio of a table based on one column?


I want to automatically backup a database so I have two databases, one with all data and the other with a ratio of each table -i.e. 20%- based on a column.


Solution

  • Here's my first try.

    create table t1 (
    id integer primary key,
    val integer not null);
    
    create table t2 (
    id integer primary key,
    val integer not null);
    
    insert into t1 values (1,1000);
    insert into t1 values (2,1000);
    insert into t1 values (3,1000);
    insert into t1 values (4,1000);
    insert into t1 values (5,1000);
    insert into t1 values (6,1000);
    insert into t1 values (7,1000);
    insert into t1 values (8,1000);
    insert into t1 values (9,1000);
    insert into t1 values (10,1000);
    insert into t1 values (11,1000);
    
    insert into t2
    select id, val
    from t1
    where (val >= 1000) 
      and (rownum <= cast((0.2 * (select count(*) from t1)) as integer));