SQL:计算每列中不同值的数量

编程入门 行业动态 更新时间:2024-10-11 03:23:14
本文介绍了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:计算每列中不同值的数量

本文发布于:2023-11-22 02:06:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数量   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!