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;
![](http://dl2.iteye.com/upload/attachment/0102/6680/c365ff12-edee-33b7-9e98-4e41c0c59670.jpg)
![](http://dl2.iteye.com/upload/attachment/0102/6682/0e7ad802-1831-3c62-a659-79937a1ae9c0.jpg)
![](http://dl2.iteye.com/upload/attachment/0102/6684/69fdf69d-7901-3159-86e6-c77e448b41c3.jpg)
![](http://dl2.iteye.com/upload/attachment/0102/6686/611d0167-8a96-3348-8dcb-b4f1db7f1309.jpg)
![](http://dl2.iteye.com/upload/attachment/0102/6688/82da2d60-f291-3f49-a608-50bffff4a55f.jpg)
![](http://dl2.iteye.com/upload/attachment/0102/6690/f283231b-84d9-334e-b6ce-79e3aa6b6443.jpg)
![](http://dl2.iteye.com/upload/attachment/0102/6692/51c238b9-8459-3a20-ac67-4bae3b8c32b4.jpg)
![](http://dl2.iteye.com/upload/attachment/0102/6694/5deac01c-4dbd-3c72-a509-84badb72345f.jpg)
![](http://dl2.iteye.com/upload/attachment/0102/6696/dcc805b2-4756-3ef3-8533-8a7716067b18.jpg)
![](http://dl2.iteye.com/upload/attachment/0102/6698/bdf182ac-17f4-3f29-9365-3f6c5975c4b7.jpg)