#!/bin/sh
priv_user="user"
priv_pass="pass"
x=`sqlplus -s ${priv_user}/${priv_pass} << EOF
set feed off
set pause off
set head off
select owner from dba_tables where table_name = 'EP_PAY_STUBS';
EOF`
iter=1
script_file="/tmp/script.sql"
echo "" > ${script_file}
for o in ${x}
do
if [ ${iter} -ne 1 ] ; then
echo "union all" >> ${script_file}
else
iter=2
fi
echo "select '${o}', to_char(trunc(display_date, 'MM'), 'YYYY-MON'), count(1) from ${o}.ep_pay_stubs where display_date between to_date('01012019','mmddyyyy') and to_date('10312019235959','mmddyyyyhh24miss') group by to_char(trunc(display_date, 'MM'), 'YYYY-MON')" >> ${script_file}
done
echo ";" >> ${script_file}
sqlplus ${priv_user}/${priv_pass} @${script_file}
Again, that is a framework. There is no error checking and user and password should really not be used that way, but it will work.
with cte as (
select
xmlelement("root",
xmlagg(
xmltype(
dbms_xmlgen.getxml('select to_char(display_date, ''YYYY-MON'') as year_mon, count(*) month_count from ' ||
owner || '.' || table_name ||
' WHERE display_date BETWEEN to_date(''01/01/2019'',''MM/DD/YYYY'') AND TO_DATE(''10/31/2019 23:59:59'',''MM/DD/YYYY HH24:MI:SS'')' ||
' group by to_char(display_date, ''YYYY-MON'')'
)
)
)
) myxml
from all_tables
where table_name = 'EP_PAY_STUBS'
)
select year_month, sum(month_count)
from cte, xmltable(
'/root/ROWSET/ROW'
passing myxml
columns
year_month varchar2(8) path 'YEAR_MON',
month_count number path 'MONTH_COUNT'
)
group by year_month
order by to_date(year_month,'YYYY-MON')
/
DECLARE
startDate DATE := to_date('01/01/2019', 'mm/dd/yyyy');
endDate DATE := to_date('10/31/2019', 'mm/dd/yyyy');
month_cnt number := 0;
year_month date := SYSDATE;
BEGIN
FOR cur_schema IN (select upper(schema) as "SCHEMA" from epayrolladmin.schema_translate t where t.employer_code in (10001,10002,10639))
loop
execute immediate 'select to_char(display_date, "YYYY-MON"), count(*) from ' || cur_schema.SCHEMA || '.ep_pay_stubs ' ||
'WHERE trunc(display_date) BETWEEN ' || startDate || ' AND ' || endDate || ' group by to_char(display_date, "YYYY-MON")'
into year_month, month_cnt;
dbms_output.put_line (cur_schema.SCHEMA || ' ' || month_cnt);
END LOOP;
END;
/
show errors;
execute immediate 'select to_char(display_date, ''YYYY-MON''), count(*) from ' || cur_schema.SCHEMA || '.ep_pay_stubs ' ||
'WHERE trunc(display_date) BETWEEN ' || startDate || ' AND ' || endDate || ' group by to_char(display_date, ''YYYY-MON'')'
into year_month, month_cnt;
DECLARE
startDate varchar2(10) := '01/01/2019';
endDate varchar2(10) := '01/01/2019';
month_cnt number := 0;
year_month varchar2(8);
BEGIN
FOR cur_schema IN (select upper(schema) as SCHEMA from epayrolladmin.schema_translate t where t.employer_code in (10001,10002,10639))
execute immediate 'select to_char(display_date, ''YYYY-MON''), count(*) from ' || cur_schema.SCHEMA || '.ep_pay_stubs ' ||
'WHERE trunc(display_date) BETWEEN to_date(:startDate, ''mm/dd/yyyy'') AND to_date(:endDate,''mm/dd/yyyy'') group by to_char(display_date, ''YYYY-MON'')'
into year_month, month_cnt
using startDate, endDate;
dbms_output.put_line (cur_schema.SCHEMA || ' ' || month_cnt);
END LOOP;
END;
/
DECLARE
startDate varchar2(10) := '01/01/2019';
endDate varchar2(10) := '10/31/2019';
month_cnt number := 0;
year_month varchar2(8);
BEGIN
FOR cur_schema IN (select upper(schema) as SCHEMA from epayrolladmin.schema_translate t where t.employer_code in (10001,10002,10639))
LOOP
execute immediate 'select to_char(display_date, ''YYYY-MON''), count(*) from ' || cur_schema.SCHEMA || '.ep_pay_stubs ' ||
'WHERE trunc(display_date) BETWEEN to_date(:startDate, ''mm/dd/yyyy'') AND to_date(:endDate,''mm/dd/yyyy'') group by to_char(display_date, ''YYYY-MON'')'
into year_month, month_cnt
using startDate, endDate;
dbms_output.put_line (cur_schema.SCHEMA || ' ' || month_cnt);
END LOOP;
--dbms_output.put_line('DELETED ' || total_records_cnt || ' TOTAL RECORDS!!');
END;
/
with cte as (
select
xmlelement("root",
xmlagg(
dbms_xmlgen.getxmltype('select to_char(display_date, ''YYYY-MON'') as year_mon, count(*) month_count from ' ||
owner || '.' || table_name ||
' WHERE display_date BETWEEN to_date(''01/01/2019'',''MM/DD/YYYY'') AND TO_DATE(''10/31/2019 23:59:59'',''MM/DD/YYYY HH24:MI:SS'')' ||
' group by to_char(display_date, ''YYYY-MON'')'
)
)
) myxml
from all_tables
where table_name = 'EP_PAY_STUBS'
)
select year_month, sum(month_count)
from cte, xmltable(
'/root/ROWSET/ROW'
passing myxml
columns
year_month varchar2(8) path 'YEAR_MON',
month_count number path 'MONTH_COUNT'
)
group by year_month
order by to_date(year_month,'YYYY-MON')
/
create or replace procedure monthly_totals (start_date in date, end_date in date) as
cursor get_schemas is
select owner from dba_tab_columns
where column_name = 'DISPLAY_DATE'
order by owner;
v_owner varchar2(30);
v_yrmon varchar2(14);
v_month varchar2(3);
v_count pls_integer;
v_jantot pls_integer := 0;
v_febtot pls_integer := 0;
v_martot pls_integer := 0;
v_aprtot pls_integer := 0;
v_maytot pls_integer := 0;
v_juntot pls_integer := 0;
v_jultot pls_integer := 0;
v_augtot pls_integer := 0;
v_septot pls_integer := 0;
v_octtot pls_integer := 0;
v_novtot pls_integer := 0;
v_dectot pls_integer := 0;
qry_prefix varchar2(80) := 'select to_char(display_date, ''YYYY-MM-MON''), count(*) from ';
qry_suffix varchar2(160) := '.ep_pay_stubs WHERE display_date BETWEEN start_date AND '
||'trunc(end_date) + 86399/86400 group by to_char(display_date, ''YYYY-MM-MON'') order by 1');
full_text varchar2(270);
begin
open get_schemas;
loop
fetch get_schemas into v_owner;
exit when get_schemas%notfound;
full_text := qry_prefix||v_owner||qry_suffix;
execute immediate(full_text) into v_yrmon, v_count;
dbms_output.put_line(substr(v_yr_mon,1,5||substr(v_yr_mon,9-3)||to_char(v_count,'999,999');
if substr(v_yr_mon,6,2) = '01' then
v_jantot := v_jantot + v_count;
elsif substr(v_yr_mon,6,2) = '02' then
v_febtot := v_febtot + v_count;
elsif substr(v_yr_mon,6,2) = '03' then
v_martot := v_martot + v_count;
elsif substr(v_yr_mon,6,2) = '04' then
v_aprtot := v_aprtot + v_count;
elsif substr(v_yr_mon,6,2) = '05' then
v_maytot := v_maytot + v_count;
elsif substr(v_yr_mon,6,2) = '06' then
v_juntot := v_juntot + v_count;
elsif substr(v_yr_mon,6,2) = '07' then
v_jultot := v_jultot + v_count;
elsif substr(v_yr_mon,6,2) = '08' then
v_augtot := v_augtot + v_count;
elsif substr(v_yr_mon,6,2) = '09' then
v_septot := v_septot + v_count;
elsif substr(v_yr_mon,6,2) = '10' then
v_octtot := v_octtot + v_count;
elsif substr(v_yr_mon,6,2) = '11' then
v_novtot := v_novtot + v_count;
else then
v_dectot := v_dectot + v_count;
end if;
end loop;
close get_schemas;
-- Now print the totals for each month:
dbms_output.put_line('Jan '||to_char(v_jantot,'99,999,999');
dbms_output.put_line('Feb '||to_char(v_febtot,'99,999,999');
dbms_output.put_line('Mar '||to_char(v_martot,'99,999,999');
dbms_output.put_line('Apr '||to_char(v_aprtot,'99,999,999');
dbms_output.put_line('May '||to_char(v_maytot,'99,999,999');
dbms_output.put_line('Jun '||to_char(v_juntot,'99,999,999');
dbms_output.put_line('Jul '||to_char(v_jultot,'99,999,999');
dbms_output.put_line('Aug '||to_char(v_augtot,'99,999,999');
dbms_output.put_line('Sep '||to_char(v_septot,'99,999,999');
dbms_output.put_line('Oct '||to_char(v_octtot,'99,999,999');
dbms_output.put_line('Nov '||to_char(v_novtot,'99,999,999');
dbms_output.put_line('Dec '||to_char(v_dectot,'99,999,999');
end;
/
create or replace procedure test_owners as
cursor get_schemas is
select owner from dba_tab_columns
where column_name = 'DISPLAY_DATE'
order by owner;
v_owner varchar2(30);
begin
open get_schemas;
loop
fetch get_schemas into v_owner;
exit when get_schemas%notfound;
dbms_output.put_line(v_owner);
end loop;
close get_schemas;
end;
/
create or replace procedure test_owners as
cursor get_schemas is
select owner from dba_tab_columns
where column_name = 'DISPLAY_DATE'
and table_name = 'EP_PAY_STUBS'
order by owner;
v_owner varchar2(30);
begin
open get_schemas;
loop
fetch get_schemas into v_owner;
exit when get_schemas%notfound;
dbms_output.put_line(v_owner);
end loop;
close get_schemas;
end;
/
loop
fetch get_schemas into v_owner;
exit when get_schemas%notfound;
full_text := qry_prefix||v_owner||qry_suffix;
begin
execute immediate(full_text) into v_yrmon, v_count;
dbms_output.put_line(substr(v_yrmon,1,5||substr(v_yrmon,9-3)||to_char(v_count,'999,999');
if substr(v_yrmon,6,2) = '01' then
v_jantot := v_jantot + v_count;
elsif substr(v_yrmon,6,2) = '02' then
v_febtot := v_febtot + v_count;
elsif substr(v_yrmon,6,2) = '03' then
v_martot := v_martot + v_count;
elsif substr(v_yrmon,6,2) = '04' then
v_aprtot := v_aprtot + v_count;
elsif substr(v_yrmon,6,2) = '05' then
v_maytot := v_maytot + v_count;
elsif substr(v_yrmon,6,2) = '06' then
v_juntot := v_juntot + v_count;
elsif substr(v_yrmon,6,2) = '07' then
v_jultot := v_jultot + v_count;
elsif substr(v_yrmon,6,2) = '08' then
v_augtot := v_augtot + v_count;
elsif substr(v_yrmon,6,2) = '09' then
v_septot := v_septot + v_count;
elsif substr(v_yrmon,6,2) = '10' then
v_octtot := v_octtot + v_count;
elsif substr(v_yrmon,6,2) = '11' then
v_novtot := v_novtot + v_count;
else then
v_dectot := v_dectot + v_count;
end if;
exception
when others then
dbms_output.put_line(sqlerrm)
dbms_output.put_line('.on schema: '||v_owner)
end;
end loop;
dbms_output.put_line(substr(full_text,1,75);
dbms_output.put_line(substr(full_text,76,150);
dbms_output.put_line(substr(full_text,151,225);
dbms_output.put_line(substr(full_text,226,300);
raise;
SCHEMA_1 2019-JAN 1
SCHEMA_2 2019-JAN 1
SCHEMA_3 2019-JAN 1
SCHEMA_1 2019-FEB 2
SCHEMA_2 2019-MAR 3
SCHEMA_3 2019-APR 4
SCHEMA_1 2019-MAY 600000
2019-JAN 3
2019-FEB 2
2019-MAR 3
2019-APR 4
2019-MAY 600000
Do you have a preference for doing this in SQL (where you would use a query from all_tab_columns to generate the list of queries for each table) or in PL\SQL, where you would use a cursor loop based on all_tab_columns, then an "execute immediate..." query from each of these tables?
What exactly should be "grouped by month"? Is that the counts from each table individually? Or, do want the counts from all tables to be combined first, then "grouped by month"?