SQL:计算每列中不同值的数量 [英] SQL: count number of distinct values in every column

查看:1026
本文介绍了SQL:计算每列中不同值的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个查询,该查询将返回一个表,其中每一列是另一张表的列中不同值的计数.

I need a query that will return a table where each column is the count of distinct values in the columns of another table.

我知道如何在一列中计算不同的值:

I know how to count the distinct values in one column:

select count(distinct columnA) from table1;

我想我可以把它变成一个很长的选择子句:

I suppose that I could just make this a really long select clause:

select count(distinct columnA), count(distinct columnB), ... from table1;

但这不是很优雅,并且是硬编码的.我想要更灵活的东西.

but that isn't very elegant and it's hardcoded. I'd prefer something more flexible.

推荐答案

我感谢所有答复.我认为在这种情况下最适合我的解决方案如下(从不知道该表的外部程序中计算出该表的每个列中不同值的数量)(

I appreciate all of the responses. I think the solution that will work best for me in this situation (counting the number of distinct values in each column of a table from an external program that has no knowledge of the table except its name) is as follows:

运行描述表1",然后从结果中拉出列名.

Run "describe table1" and pull out the column names from the result.

浏览各列名称,并创建查询以计算各列中的不同值.查询将类似于从表1中选择count(distinct columnA),count(distinct columnB),...".

Loop through the column names and create the query to count the distinct values in each column. The query will look something like "select count(distinct columnA), count(distinct columnB), ... from table1".

这篇关于SQL:计算每列中不同值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆