How to get the min, max and sum of all columns in the database?

aymen ben abdallah

I am trying to get the min, max and sum of all columns in the database, but I am stuck on an error. I am trying to insert into a table the min and max if data_type column in ('VARCHAR2','TIMESTAMP(6)','CHAR','DATE','INTERVAL DAY(9) TO SECOND(9)','INTERVAL DAY(2) TO SECOND(6)') and if it is a 'Number' to calculate the sum, if neither of both, it should write '###'

create or replace PROCEDURE COMPARAISON_TEST AS 
BEGIN
declare

cursor c_tab is

select all_tab_cols.owner
, all_tab_cols.table_name
, all_tab_cols.column_name
, all_tab_cols.data_type
, all_tables.num_rows
from all_tab_cols 
left join all_tables on 
all_tab_cols.table_name = all_tables.table_name
where all_tab_cols.data_type in ('VARCHAR2','TIMESTAMP(6)','CHAR','DATE','INTERVAL DAY(9) TO 
SECOND(9)','INTERVAL DAY(2) TO SECOND(6)','NUMBER')
and all_tab_cols.table_name not like '\_%' escape '\' 
and all_tab_cols.owner in ('ODS','DWH','DQM','SCL','STG')
and all_tables.num_rows not in ('0');

v_sql VARCHAR2 (300);
v_val VARCHAR2 (300);
v_sqln VARCHAR2 (300);
v_valn VARCHAR2 (300);
v_sqls VARCHAR2 (300);
v_vals VARCHAR2 (300);

dat_situ date;

begin
dat_situ := current_date;

for r_tab in c_tab loop

if r_tab.data_type = 'VARCHAR2' and 
all_tab_cols.data_type = 'TIMESTAMP(6)' and
all_tab_cols.data_type = 'CHAR' and
all_tab_cols.data_type = 'DATE' and
all_tab_cols.data_type = 'INTERVAL DAY(9) TO SECOND(9)' and
all_tab_cols.data_type = 'INTERVAL DAY(2) TO SECOND(6)'
then

v_sql := 'select max('||r_tab.column_name||') from '||r_tab.owner||'.'||r_tab.table_name;
v_sqln := 'select min('||r_tab.column_name||') from '||r_tab.owner||'.'||r_tab.table_name;

else if all_tab_cols.data_type = 'NUMBER'
then

v_sqls := 'select sum('||r_tab.column_name||') from '||r_tab.owner||'.'||r_tab.table_name;
else

v_sql := '###';
v_sqln := '###';
v_sqls := '###';


execute immediate v_sql into v_val;
execute immediate v_sqln into v_valn;
execute immediate v_sqls into v_vals;

DBMS_OUTPUT.ENABLE(buffer_size => null);
dbms_output.put_line(r_tab.owner||' : '||r_tab.table_name||' : '||r_tab.column_name||' : '|| v_val||' : '||v_valn||' : '||v_vals||' : '||dat_situ||' : '||r_tab.num_rows);

insert into MOE_COLMINMAX (owner,table_name,column_name,column_max,column_min,dat_situ,num_rows)
values (r_tab.owner,r_tab.table_name,r_tab.column_name,v_val,v_valn,dat_situ,r_tab.num_rows);
commit;
end if;
end loop;
end;
END COMPARAISON_TEST;

ERRORS 
Erreur(67,5): PLS-00103: Symbole "LOOP" rencontré à la place d'un des symboles suivants :     if 
Erreur(69,5): PLS-00103: Symbole "COMPARAISON_TEST" rencontré à la place d'un des symboles suivants :     ; 
aymen ben abdallah

all good, i made it out here is the code to whom may need it

create or replace PROCEDURE COMPARAISON_TEST_MINMAX AS 
BEGIN
declare
--                          cursor pour l'ecriture des MIN MAX pour toute les cahmps 
de toute les tables de la base de donnée
--                          recupération des nom des tables et leurs nom des colonns
--                          filtrage par type de données -- fuire la tiret bas qui 
existe au debut du quelque nom de tables
cursor c_tab is
select all_tab_cols.owner
 , all_tab_cols.table_name
 , all_tab_cols.column_name
 , all_tab_cols.data_type
 , all_tables.num_rows
from all_tab_cols 
left join all_tables on 
all_tab_cols.table_name = all_tables.table_name
where all_tab_cols.data_type in ('VARCHAR2','TIMESTAMP(6)','CHAR','DATE','INTERVAL         
DAY(9) TO SECOND(9)','INTERVAL DAY(2) TO SECOND(6)','NUMBER','DECIMAL','DOUBLE')
and all_tab_cols.table_name not like '\_%' escape '\' 
and all_tab_cols.owner in ('ODS','DWH','DQM','SCL','STG')
and all_tables.num_rows not in ('0');
--                          1er variable v_sql pour enregistrer la valeur maximale des colonnes
--                          2eme variable v_val pour recuperer la valeur de variable v_sql
--                          3eme variable v_sqln pour enregistrer la valeur minimale des colonnes
--                          4eme variable v_valn pour recuperer la valeur de variable v_sqln
--                          5eme variable v_sqls pour enregistrer la valeur somme des colonnes
--                          6eme variable v_valn pour recuperer la valeur de somme v_sqln
--                          7eme variable v_sqls pour enregistrer la valeur count des colonnes
--                          8eme variable v_valn pour recuperer la valeur de count v_sqln
v_sql VARCHAR2 (300);
v_val VARCHAR2 (300);
v_sqln VARCHAR2 (300);
v_valn VARCHAR2 (300);
v_sqls VARCHAR2 (300);
v_vals VARCHAR2 (300);
v_sqlz VARCHAR2 (300);
v_valz VARCHAR2 (300);
--                          definir dat_situ qui prend la date de lexecution de ce code
dat_situ date;
begin
dat_situ := current_date;
--                          une boucle pour passer par toutes les colonnes de toutes les table de base de donnée
for r_tab in c_tab loop
--                          case: condition sur les data type
case 
when r_tab.data_type in ('VARCHAR2','TIMESTAMP(6)','CHAR','DATE','INTERVAL DAY(9) TO     
SECOND(9)','INTERVAL DAY(2) TO SECOND(6)') then

--                          select du max et min 
v_sql := 'select max('||r_tab.column_name||') from '||r_tab.owner||'.'||r_tab.table_name; 
v_sqln := 'select min('||r_tab.column_name||') from '||r_tab.owner||'.'||r_tab.table_name;
v_vals := null;
--                          affecter les valeur des variables
execute immediate v_sql into v_val;
execute immediate v_sqln into v_valn;
else
--                          select somme
v_sqls := 'select sum('||r_tab.column_name||') from '||r_tab.owner||'.'||r_tab.table_name;
v_val := null;
v_valn := null;
--                          affecter les valeur des variables
execute immediate v_sqls into v_vals;
end case;
--                          count sur toutes les tables
v_sqlz := 'select count(*) from '||r_tab.owner||'.'||r_tab.table_name;
--                          affecter les valeur des variables
execute immediate v_sqlz into v_valz;
--                          query string et trop long ceci pour augmenter la taille du     buffer
DBMS_OUTPUT.ENABLE(buffer_size => null);
dbms_output.put_line(r_tab.owner||' : '||r_tab.table_name||' : '||r_tab.column_name||' : '|| v_val||' : '||v_valn||' : '||v_vals||' : '||v_valz||' : '||dat_situ);
--                          enregistrer toutes les données recuperée dans une table   existante dans la base de donnée
insert into MOE_COLMINMAX(owner,table_name,column_name,column_max,column_min,column_sum,num_rows,dat_situ)
values     (r_tab.owner,r_tab.table_name,r_tab.column_name,v_val,v_valn,v_vals,v_valz,dat_situ);
commit;
end loop;
end;
END COMPARAISON_TEST_MINMAX;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get all the result columns from database with other custom(concat, sum, count) columns in Jooq

How to get sum of all max column value

How to get Mysql SUM of multiple columns in database

How to get max and min of columns per row for pivoted table?

in MS EXCEL how to get the min and max on multiple columns by grouping

How to get the sum of FreeSpace of all the tables in a database

How to get Max of sum of two columns, with associated column - SQL Live

How to apply aggregations(sum,mean,max,min etc ) across columns in pydatatable?

Get min / max grouped by two columns

SQL: Get all columns on MAX

How to get the min value for the first two columns and the max value for the next two in a list of 4 columns in python?

PostgreSQL - Get all rows with min max value

How to show in the select menu only the values between "min_participants"and "max_participants" database columns values?

sum two columns, calculate max, min and mean value in MapReduce

Consolidate Rows Using Dictionary In VBA with Sum and Max/Min to certain columns

How to get the MAX() while NOT grouping all columns in SQL Server

How to get max and min of generated random numbers and find the sum of the two in Eclipse

Compute sum of (min*max) across all subarrays of the array

Sum all numeric columns in database and log results

how get the max of sum in access?

How can we get file name, sheet name, max rows, and max columns for all Excel files in a folder?

how to divide all columns by sum of columns

How to get min and max value of primitives?

How to get min and max values of ranges with numpy?

How get values inside min(), max() in zod?

How to get max and min values of an array with spaces?

How to get the min/max value of Enum in TypeScript

How to get the correct min and max values

How to get the max/min value of a String[] array?