sql - 分别计算每列的不同值

标签 sql oracle count distinct

我可以在不枚举的情况下计算每一列的不同值吗?

假设我有一个包含 col1col2col3 且没有其他列的表。在不明确提及这些列的情况下,我希望得到与以下相同的结果:

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/

相关文章:

sql - 从文本 regexp_substr 中删除第二个引号

java - ORA-01795 : maximum number of expressions in a list is 1000 error with myibatis

sql - 在 PL/SQL 中使用带有 "LIKE %"(例如 "variable%")的变量?

Java 8 计算具有多个值的对象

python - Pandas (如何修复): List is actually string and the value of length is misleading

tsql - 计算每个用户的有效值

mysql - 即使 NOT NULL 也为 Null

mysql - 基于字段更改外键

c# - 如何使用 Entity Framework 执行插入查询

sql - 为什么这个序列增加了 2?