请在下面找到脚本生成的SQL查询:
declare
v_col varchar2(64) := 'run_id';
v_val varchar2(64) := '205572';
v_table varchar2(64) := 'EAP_FACT';
v_schema varchar2(64) := 'EBL_CLIENT';
begin
dbms_output.put_line('select *'||chr(10)||'from (select ');
for i in (select t.COLUMN_NAME, rownum rn
from all_tab_columns t
where t.TABLE_NAME = upper(v_table)
and t.OWNER = upper(v_schema)
and t.COLUMN_NAME <> upper(v_col)
order by t.COLUMN_ID)
loop
dbms_output.put_line(' '||case when i.rn=1 then ' ' else ',' end||
'count(distinct '||i.column_name||') '||i.column_name);
end loop;
dbms_output.put_line(' from '||v_schema||'.'||v_table||' t where t.'||v_col||' = '''||v_val||''')'
||chr(10)||'unpivot'||chr(10)||'(cnt');
for i in (select listagg (t.COLUMN_NAME,',') within group (order by t.COLUMN_ID) lst
from all_tab_columns t
where t.TABLE_NAME = upper(v_table)
and t.OWNER = upper(v_schema)
and t.COLUMN_NAME <> upper(v_col))
loop
dbms_output.put_line(' '||'for col in ('||i.lst||')');
end loop;
dbms_output.put_line(')'||chr(10)||'order by cnt desc');
end;
你会得到一些这样的查询:
select *
from (select
count(distinct t.field1) field1
,count(distinct t.field2) field2
,count(distinct t.field3) field3
from EBL_CLIENT.EAP_FACT t where t.run_id = '205572')
unpivot
(cnt
for col in (field1,field2,field3)
)
order by cnt desc
并运行后,此查询的结果会是这样:
col cnt
field2 5
field1 3
field3 1
这通常是用'SE LECT列,COUNT(*)FROM表GROUP BY列。 – Kenney
可能很难获得** all **列的值的计数,因为如果列的类型为CLOB,LOB,BLOB,LONG,XML,Object,嵌套表格等,则无法获得明确的计数。 – krokodilko
是的,我只是想尽量减少我必须运行的查询。我在SQL方面经验不足,不知道是否有可运行的脚本,可以为EAP_FACT表提供每个字段的不同值计数。有117个不同的领域。 – dag06001