oracle 如何获得数据库行中字符串的唯一长度的计数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8185145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:26:57  来源:igfitidea点击:

How can an get count of the unique lengths of a string in database rows?

sqloracle

提问by johnny

I am using Oracle and I have a table with 1000 rows. There is a last name field and

我正在使用 Oracle 并且我有一个包含 1000 行的表。有一个姓氏字段和

I want to know the lengths of the name field but I don't want it for every row. I want a count of the various lengths.

我想知道 name 字段的长度,但我不希望每一行都有它。我想要各种长度的计数。

Example:

例子:

lastname:

姓:

smith
smith
Johnson
Johnson
Hymanson
Baggins

There are two smiths length of five. Four others, length of seven. I want my query to return

有两个史密斯长度为五。其他四个,长度为七。我希望我的查询返回

7
5

If there were 1,000 names I expect to get all kinds of lengths.

如果有 1,000 个名字,我希望得到各种长度。

I tried,

我试过,

Select count(*) as total, lastname from myNames group by total

It didn't know what total was. Grouping by lastname just groups on each individual name unless it's a different last name, which is as expected but not what I need.

它不知道总数是多少。按姓氏分组只是对每个单独的名字进行分组,除非它是不同的姓氏,这是预期的,但不是我需要的。

Can this be done in one SQL query?

这可以在一个 SQL 查询中完成吗?

回答by Joel Coehoorn

SELECT Length(lastname)
FROM MyTable
GROUP BY Length(lastname)

回答by Chris

select distinct(LENGTH(lastname)) from mynames;

回答by Goutham

Select count(*), Length(column_name) from table_name group by Length(column_name);

This will work for the different lengths in a single column.

这将适用于单个列中的不同长度。