如何统计出一个字段中出现次数最多的字符。

Amphibian 2003-07-15 08:55:49
如题。请大家指点。
...全文
186 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2003-07-15
  • 打赏
  • 举报
回复
如果是第二种,就用:
select top 1 字段 ,count(*) 字符出现的次数 from 表 group by 字段 order by count(*) desc
zjcxc 2003-07-15
  • 打赏
  • 举报
回复
如果是第一种,就要将字段中的字符分拆后,进行统计:
--创建数据测试环境
create table #tb(a varchar(200))
insert into #tb
select 'abcdasdlfjaslk;dfqwoieurwhft'
union all select 'a;sldkjfal;sopqwhtdlkdafrfgsgasdfh'
union all select 'asldfkworufgsea87y9oqwpe tchgwccmnqnw3 '

--为字符分拆准备临时表,top 200 是根据要分拆的字符串的最大长度而定的,
--在测试中,因为a字段的最大为200,故这里用 top 200
select top 200 id=identity(int,1,1)
into #aa from
(select top 100 id from syscolumns) a
,(select top 100 id from syscolumns) b
,(select top 100 id from syscolumns) c

--得到结果
select top 1 substring(a,id,1)
from #tb a,#aa b
where substring(a.a,b.id,1)<>''
group by substring(a.a,b.id,1)
order by sum(1) desc

drop table #tb,#aa
zjcxc 2003-07-15
  • 打赏
  • 举报
回复
你说清楚一点:
是指字符串中出现最多的字符,还是字段中字符最多的字段?
friendliu 2003-07-15
  • 打赏
  • 举报
回复
你说的出现最多的字符是你给出的字符
还是从字段中去统计找出最多的字符呀
pengdali 2003-07-15
  • 打赏
  • 举报
回复
举例:

create table #a(a varchar(8000))
insert #a values('sdfdrtyrfgh')
insert #a values('wersmvcmlj')
insert #a values('xlkcjfoziesre')

select top 8000 identity(int,1,1) id into #dali from sysobjects a,sysobjects b

select top 1 substring(a,id,1) from #a aa,#dali bb where substring(a,id,1)<>'' group by substring(a,id,1) order by count(*) desc


drop table #a,#dali
jackiedlh 2003-07-15
  • 打赏
  • 举报
回复
是英文字符吗?

用存储过程的case语句可方便实现
zosky 2003-07-15
  • 打赏
  • 举报
回复
lggege8000 2003-07-15
  • 打赏
  • 举报
回复
select top 1 字段 ,count(*) 字符出现的次数 from 表 group by 字段 order by count(*) desc
txlicenhe 2003-07-15
  • 打赏
  • 举报
回复
有点难,可能要用游标才行
pengdali 2003-07-15
  • 打赏
  • 举报
回复
select top 1 字段 from 表 group by 字段 order by count(*) desc

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧