分别计算每列的不同值

时间:2017-12-11 14:23:08

标签: sql oracle count distinct

我可以计算每列的不同值而不计算它们吗?

假设我的表格包含col1col2col3,而且没有其他列。如果没有明确提及这些列,我希望得到与以下结果相同的结果:

SELECT
count(distinct col1) as col1,
count(distinct col2) as col2,
count(distinct col3) as col3
FROM mytable;

我该怎么做?

1 个答案:

答案 0 :(得分:2)

我认为使用纯SQL可以轻松完成的最好方法是运行这样的查询来生成所需的查询,然后运行它。

select 'select count(distinct '
    || listagg(column_name || ') as ' || column_name, ', count(distinct ') within group (order by column_id) 
    || ' from ' || max(table_name) || ';' as script
from all_tab_cols
where table_name = 'MYTABLE';
相关问题