Home > Net >  Calculate record completeness of a table
Calculate record completeness of a table

Time:02-23

hi i am doing a thesis project: a database analyzer. In this case, I connect an oracle database and I want to calculate the total completeness of the records of a table. Then I have the user select the table and I would like to calculate this characteristic: RECORD COMPLETENESS --> X=A/B

A= number of data items with associated value not null in a record

B= number of data items of the record for which completeness can be measured. How can i do it? any ideas? i'm coding in c# and the database that i'm analyzing is oracle.

CodePudding user response:

I don't know C, so I'll talk about Oracle.

You might create a function that calculates that number as follows:

SQL> create or replace function f_comp (par_table_name in varchar2)
  2    return number
  3  is
  4    l_total_rows number;
  5    l_total_cols number;
  6    l_str        varchar2(500);
  7    l_cnt        number;
  8    l_total      number := 0;
  9    retval       number;
 10  begin
 11    execute immediate 'select count(*) from ' ||
 12      dbms_assert.sql_object_name(par_table_name)
 13      into l_total_rows;
 14
 15    select count(*)
 16      into l_total_cols
 17      from user_tab_columns
 18      where upper(table_name) = upper(par_table_name);
 19
 20    for cur_r in (select column_name from user_tab_columns
 21                  where upper(table_name) = upper(par_table_name)
 22                 )
 23    loop
 24      l_str := 'select count(*) from ' || par_table_name ||
 25               ' where ' || cur_r.column_name || ' is not null';
 26      execute immediate l_str into l_cnt;
 27      l_total := l_total   l_cnt;
 28    end loop;
 29
 30    retval := round(l_total / (l_total_rows * l_total_cols) * 100, 2);
 31    return retval;
 32  end;
 33  /

Function created.

Testing:

Scott's dept table has all columns filled with data, so - completeness is 100%:

SQL> select * from dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

SQL> select f_comp('dept') result from dual;

    RESULT
----------
       100

SQL>

emp, on the other hand, doesn't have comm column for all rows and the president King doesn't have a manager, so completeness is lower:

SQL> select * from emp;

     EMPNO ENAME      JOB              MGR HIREDATE        SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17.12.80        800                    20
      7499 ALLEN      SALESMAN        7698 20.02.81       1600        300         30
      7521 WARD       SALESMAN        7698 22.02.81       1250        500         30
      7566 JONES      MANAGER         7839 02.04.81       2975                    20
      7654 MARTIN     SALESMAN        7698 28.09.81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01.05.81       2850                    30
      7782 CLARK      MANAGER         7839 09.06.81       2450                    10
      7788 SCOTT      ANALYST         7566 09.12.82       3000                    20
      7839 KING       PRESIDENT            17.11.81       5000                    10
      7844 TURNER     SALESMAN        7698 08.09.81       1500          0         30
      7876 ADAMS      CLERK           7788 12.01.83       1100                    20
      7900 JAMES      CLERK           7698 03.12.81        950                    30
      7902 FORD       ANALYST         7566 03.12.81       3000                    20
      7934 MILLER     CLERK           7782 23.01.82       1300                    10

14 rows selected.

SQL> select f_comp('emp')  result from dual;

    RESULT
----------
     90,18

SQL>
  • Related