-- Master table
CREATE
    TABLE tpm_wind_dcn
    (
        router CHARACTER VARYING(6),
        interface CHARACTER VARYING(8),
        data DATE,
        qora NUMERIC(2,0),
        m1 NUMERIC(19,2),
        m2 NUMERIC(19,2),
        m3 NUMERIC(19,2),
        m4 NUMERIC(19,2),
        m5 NUMERIC(19,2),
        m6 NUMERIC(19,2),
        PRIMARY KEY (router, interface, data, qora)
    );

CREATE TABLE tp_wind_dcn_day1 ( 
     CHECK ( data >= DATE '2010-01-01' AND data < DATE '2010-01-02' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day2 ( 
     CHECK ( data >= DATE '2010-01-02' AND data < DATE '2010-01-03' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day3 ( 
     CHECK ( data >= DATE '2010-01-03' AND data < DATE '2010-01-04' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day4 ( 
     CHECK ( data >= DATE '2010-01-04' AND data < DATE '2010-01-05' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day5 ( 
     CHECK ( data >= DATE '2010-01-05' AND data < DATE '2010-01-06' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day6 ( 
     CHECK ( data >= DATE '2010-01-06' AND data < DATE '2010-01-07' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day7 ( 
     CHECK ( data >= DATE '2010-01-07' AND data < DATE '2010-01-08' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day8 ( 
     CHECK ( data >= DATE '2010-01-08' AND data < DATE '2010-01-09' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day9 ( 
     CHECK ( data >= DATE '2010-01-09' AND data < DATE '2010-01-10' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day10 ( 
     CHECK ( data >= DATE '2010-01-10' AND data < DATE '2010-01-11' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day11 ( 
     CHECK ( data >= DATE '2010-01-11' AND data < DATE '2010-01-12' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day12 ( 
     CHECK ( data >= DATE '2010-01-12' AND data < DATE '2010-01-13' )
) INHERITS (tpm_wind_dcn);
--
CREATE TABLE tp_wind_dcn_day13 ( 
     CHECK ( data >= DATE '2010-01-13' AND data < DATE '2010-01-14' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day14 ( 
     CHECK ( data >= DATE '2010-01-14' AND data < DATE '2010-01-15' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day15 ( 
     CHECK ( data >= DATE '2010-01-15' AND data < DATE '2010-01-16' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day16 ( 
     CHECK ( data >= DATE '2010-01-16' AND data < DATE '2010-01-17' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day17 ( 
     CHECK ( data >= DATE '2010-01-17' AND data < DATE '2010-01-18' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day18 ( 
     CHECK ( data >= DATE '2010-01-18' AND data < DATE '2010-01-19' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day19 ( 
     CHECK ( data >= DATE '2010-01-19' AND data < DATE '2010-01-20' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day20 ( 
     CHECK ( data >= DATE '2010-01-20' AND data < DATE '2010-01-21' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day21 ( 
     CHECK ( data >= DATE '2010-01-21' AND data < DATE '2010-01-22' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day22 ( 
     CHECK ( data >= DATE '2010-01-22' AND data < DATE '2010-01-23' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day23 ( 
     CHECK ( data >= DATE '2010-01-23' AND data < DATE '2010-01-24' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day24 ( 
     CHECK ( data >= DATE '2010-01-24' AND data < DATE '2010-01-25' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day25 ( 
     CHECK ( data >= DATE '2010-01-25' AND data < DATE '2010-01-26' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day26 ( 
     CHECK ( data >= DATE '2010-01-26' AND data < DATE '2010-01-27' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day27 ( 
     CHECK ( data >= DATE '2010-01-27' AND data < DATE '2010-01-28' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day28 ( 
     CHECK ( data >= DATE '2010-01-28' AND data < DATE '2010-01-29' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day29 ( 
     CHECK ( data >= DATE '2010-01-29' AND data < DATE '2010-01-30' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day30 ( 
     CHECK ( data >= DATE '2010-01-30' AND data < DATE '2010-01-31' )
) INHERITS (tpm_wind_dcn);
CREATE TABLE tp_wind_dcn_day31 ( 
     CHECK ( data >= DATE '2010-01-31' AND data < DATE '2010-02-01' )
) INHERITS (tpm_wind_dcn);

CREATE LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION tpm_wind_dcn_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
    IF ( NEW.data >= DATE '2010-01-01' AND NEW.data < DATE '2010-01-02' ) THEN
        INSERT INTO tp_wind_dcn_day1 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-02' AND NEW.data < DATE '2010-01-03' ) THEN
        INSERT INTO tp_wind_dcn_day2 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-03' AND NEW.data < DATE '2010-01-04' ) THEN
        INSERT INTO tp_wind_dcn_day3 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-04' AND NEW.data < DATE '2010-01-05' ) THEN
        INSERT INTO tp_wind_dcn_day4 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-05' AND NEW.data < DATE '2010-01-06' ) THEN
        INSERT INTO tp_wind_dcn_day5 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-06' AND NEW.data < DATE '2010-01-07' ) THEN
        INSERT INTO tp_wind_dcn_day6 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-07' AND NEW.data < DATE '2010-01-08' ) THEN
        INSERT INTO tp_wind_dcn_day7 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-08' AND NEW.data < DATE '2010-01-09' ) THEN
        INSERT INTO tp_wind_dcn_day8 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-09' AND NEW.data < DATE '2010-01-10' ) THEN
        INSERT INTO tp_wind_dcn_day9 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-10' AND NEW.data < DATE '2010-01-11' ) THEN
        INSERT INTO tp_wind_dcn_day10 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-11' AND NEW.data < DATE '2010-01-12' ) THEN
        INSERT INTO tp_wind_dcn_day11 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-12' AND NEW.data < DATE '2010-01-13' ) THEN
        INSERT INTO tp_wind_dcn_day12 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-13' AND NEW.data < DATE '2010-01-14' ) THEN
        INSERT INTO tp_wind_dcn_day13 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-14' AND NEW.data < DATE '2010-01-15' ) THEN
        INSERT INTO tp_wind_dcn_day14 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-15' AND NEW.data < DATE '2010-01-16' ) THEN
        INSERT INTO tp_wind_dcn_day15 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-16' AND NEW.data < DATE '2010-01-17' ) THEN
        INSERT INTO tp_wind_dcn_day16 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-17' AND NEW.data < DATE '2010-01-18' ) THEN
        INSERT INTO tp_wind_dcn_day17 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-18' AND NEW.data < DATE '2010-01-19' ) THEN
        INSERT INTO tp_wind_dcn_day18 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-19' AND NEW.data < DATE '2010-01-20' ) THEN
        INSERT INTO tp_wind_dcn_day19 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-20' AND NEW.data < DATE '2010-01-21' ) THEN
        INSERT INTO tp_wind_dcn_day20 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-21' AND NEW.data < DATE '2010-01-22' ) THEN
        INSERT INTO tp_wind_dcn_day21 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-22' AND NEW.data < DATE '2010-01-23' ) THEN
        INSERT INTO tp_wind_dcn_day22 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-23' AND NEW.data < DATE '2010-01-24' ) THEN
        INSERT INTO tp_wind_dcn_day23 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-24' AND NEW.data < DATE '2010-01-25' ) THEN
        INSERT INTO tp_wind_dcn_day24 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-25' AND NEW.data < DATE '2010-01-26' ) THEN
        INSERT INTO tp_wind_dcn_day25 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-26' AND NEW.data < DATE '2010-01-27' ) THEN
        INSERT INTO tp_wind_dcn_day26 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-27' AND NEW.data < DATE '2010-01-28' ) THEN
        INSERT INTO tp_wind_dcn_day27 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-28' AND NEW.data < DATE '2010-01-29' ) THEN
        INSERT INTO tp_wind_dcn_day28 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-29' AND NEW.data < DATE '2010-01-30' ) THEN
        INSERT INTO tp_wind_dcn_day29 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-30' AND NEW.data < DATE '2010-01-31' ) THEN
        INSERT INTO tp_wind_dcn_day30 VALUES (NEW.*);
    ELSIF ( NEW.data >= DATE '2010-01-31' AND NEW.data < DATE '2010-02-01' ) THEN
        INSERT INTO tp_wind_dcn_day31 VALUES (NEW.*);
    ELSE
        RAISE EXCEPTION 'Date out of range.  Fix the tpm_wind_dcn_insert_trigger() function!';
    END IF;
    RETURN NULL;
END;
$$
LANGUAGE plpgsql;

CREATE TRIGGER insert_tpm_wind_dcn_trigger
    BEFORE INSERT ON tpm_wind_dcn
    FOR EACH ROW EXECUTE PROCEDURE tpm_wind_dcn_insert_trigger();
