我可以在不枚举的情况下计算每一列的不同值吗?
假设我有一个包含 col1
、col2
、col3
且没有其他列的表。在不明确提及这些列的情况下,我希望得到与以下相同的结果:
SELECT
count(distinct col1) as col1,
count(distinct col2) as col2,
count(distinct col3) as col3
FROM mytable;
我该怎么做?
最佳答案
我认为您可以使用纯 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';
关于sql - 分别计算每列的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47754990/