Data verification scheme based on postgresSQL-a method based on OOSE

Recently, a data integrity verification tool has been developed based on the postgresSQL cursor and temporary table scheme.

5 SQL summary
1. Record the total number of records of vendor_name in result_table
sql := 'insert into '||result_table||' (time, vendor_name, total number of records)
select time, vendor name, count(*) from '|| check_table | | ' where time=' || '''' || time || '''' || ' group by time, manufacturer name';

EXECUTE sql;

2. Extract all field names of the check_table table and store them in tmp_check_col
sql : = 'create temp table tmp_check_col as SELECT a.attname
            FROM pg_class as c,pg_attribute as a
        where c.relname = '''||check_table||''' and a.attrelid = c.oid and a.attnum>0
        and a.attname not in ( ' ||''''||'Time'||''''||' ) ';





            SELECT attname from tmp_check_col ;

4. Read the cursor and read the field names one by one in a loop
        FETCH datcheck_C INTO colname ;
        while (colname.attname is not null) loop 
       
5. In the loop body, calculate the number of empty records [num] divided by step 1 The total records in , get the missing rate of a field, and update it to the corresponding cell       
sql := 'update '||result_table||' b set '||colname.attname||'=(SELECT round(a. num::numeric/b.Total number of records::numeric,4) FROM  
        (
        select time, manufacturer name, count(*) as num from '||check_table||' where '||colname.attname||' is not null and time='''||time||''' group by time, vendor name
        ) a
       WHERE a.time=b.time and a.vendor name=b.vendor_name) where time='''||time| |'''';

EXECUTE sql;
      
sql := 'update '||result_table||' set '||colname.attname||' =0 where 时间='''||time||''' and '||colname.attname||' is null';

EXECUTE sql;


        FETCH datcheck_C INTO colname;
        END loop;




1、提取check_table表的所有字段名,存入tmp_check_col
sql := 'create temp table tmp_check_col as  SELECT a.attname
            FROM pg_class as c,pg_attribute as a
        where c.relname = '''||check_table||''' and a.attrelid = c.oid and a.attnum>0
        and a.attname not in ( ''时间'' ) ';

EXECUTE sql;         FETCH datcheck_C INTO colname ; 3. Read the cursor and read the field names one by one                        SELECT attname from tmp_check_col open datcheck_C FOR

2. Open the cursor


           


        while (colname.attname is not null) loop
4. In the body of the loop, insert a record with an empty field into result_table
        sql :='insert into '||result_table||' select * from '||check_table||' where '| |colname.attname||' is null and time='''||time||'''';
        EXECUTE sql;
5. In the body of the loop, delete records with empty fields to check_table      
        sql :='delete from '| |check_table||' b where '||colname.attname||' is null and time='''||time||'''';
        EXECUTE sql;
     
        FETCH datcheck_C INTO colname;
END loop;

CLOSE datcheck_C;           






































Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326483103&siteId=291194637