create pluggable database NEREUS admin user PDB$OWNER identified by secret;
alter pluggable database NEREUS open;
alter pluggable database NEREUS save state;

alter session set container=NEREUS;
show pdbs
show con_name

grant sysdba to adm identified by secret;

alias NEREUS='rlwrap sqlplus adm/secret@bakura/NEREUS as sysdba'

create tablespace USERS datafile size 32M autoextend ON next 32M;
alter database default tablespace USERS; 

create user HR identified by secret
  quota unlimited on USERS;

grant CONNECT,RESOURCE to HR;
grant CREATE VIEW to HR;

wget https://raw.githubusercontent.com/oracle-samples/db-sample-schemas/main/human_resources/hr_cre.sql
wget https://raw.githubusercontent.com/oracle-samples/db-sample-schemas/main/human_resources/hr_popul.sql

connect HR/secret@bakura/NEREUS

spool install.txt
@hr_cre.sql
@hr_popul.sql


alter user HR no authentication;

select /*+ GATHER_PLAN_STATISTICS */
	  emp.FIRST_NAME
	, emp.LAST_NAME
	, dept.DEPARTMENT_NAME
from 
	HR.EMPLOYEES emp,
	HR.DEPARTMENTS dept
where 
	emp.DEPARTMENT_ID = dept.DEPARTMENT_ID   
order by 
	FIRST_NAME,
	LAST_NAME
/

select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST  +PEEKED_BINDS +PARALLEL +PARTITION +COST +BYTES'));

exec dbms_stats.delete_table_stats('HR','EMPLOYEES');
exec dbms_stats.delete_table_stats('HR','DEPARTMENTS');

alter system flush shared_pool;

exec dbms_stats.gather_table_stats('HR','EMPLOYEES', method_opt=>'for all columns size SKEWONLY');
exec dbms_stats.gather_table_stats('HR','DEPARTMENTS', method_opt=>'for all columns size SKEWONLY');

exec dbms_stats.gather_table_stats('HR','EMPLOYEES', method_opt=>'for all columns size 254');
exec dbms_stats.gather_table_stats('HR','DEPARTMENTS', method_opt=>'for all columns size 254');



select column_name,num_distinct,density,num_nulls,num_buckets,sample_size,histogram  
from dba_tab_col_statistics  
where owner='HR' and table_name='EMPLOYEES' and column_name='DEPARTMENT_ID'; 

select endpoint_value as column_value, 
endpoint_number as cummulative_frequency, 
endpoint_number - lag(endpoint_number,1,0) over (order by endpoint_number) as frequency 
from dba_tab_histograms 
where owner='HR' and table_name='EMPLOYEES' and column_name='DEPARTMENT_ID'; 

select column_name,num_distinct,density,num_nulls,num_buckets,sample_size,histogram  
from dba_tab_col_statistics  
where owner='HR' and table_name='DEPARTMENTS' and column_name='DEPARTMENT_ID'; 

select endpoint_value as column_value, 
endpoint_number as cummulative_frequency, 
endpoint_number - lag(endpoint_number,1,0) over (order by endpoint_number) as frequency 
from dba_tab_histograms 
where owner='HR' and table_name='DEPARTMENTS' and column_name='DEPARTMENT_ID'; 


break on report skip 1
compute sum of product on report
column product format 999,999,999
 
with f1 as (
select
        endpoint_value                                                            value,
        endpoint_number - lag(endpoint_number,1,0) over(order by endpoint_number) frequency
from
        dba_tab_histograms
where
        owner='HR'
and     table_name  = 'EMPLOYEES'
and     column_name = 'DEPARTMENT_ID'
order by
        endpoint_value
),
f2 as (
select
        endpoint_value                                                            value,
        endpoint_number - lag(endpoint_number,1,0) over(order by endpoint_number) frequency
from
        dba_tab_histograms
where
		owner='HR'
and     table_name  = 'DEPARTMENTS'
and     column_name = 'DEPARTMENT_ID'
order by
        endpoint_value
)
select
        f1.value, f1.frequency, f2.frequency, f1.frequency * f2.frequency product
from
        f1, f2
where
        f2.value = f1.value
;
