求一个统计电话号码个数的存储过程

flashasp 2012-01-13 04:28:47
SELECT [SendTels] FROM [ZW_User_duanxin] where number=1

已知上述sql得出如下电话号码,分别是6条记录
--------------------------------------------------------------
14002727924
14002727924 14002727926
14002727924 14002727926 14002727927
84002727926 84002727927 84002727928 84002727928
84002727924 84002727926 84002727927 84002727928
14002727924 14002727926 14002727927 14002727928 84002727928
--------------------------------------------------------------

用怎样才存储过程可以得到所有电话号码的总和!重复号码也算。
...全文
1458 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanyuchonglou 2012-01-17
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 maco_wang 的回复:]
引用 20 楼 flashasp 的回复:

对了,请问楼上的兄弟 /12.00代表啥意思哦!

11位电话号码+1个空格 ,因为空格有可能出现连续的两个空格,或是开始结尾有多余的1-2个空格,所以用.00控制精度 。
[/Quote]
叶子啊,骑白马的也有可能是唐僧,不是每个问题都值得回答。
flashasp 2012-01-13
  • 打赏
  • 举报
回复

NND,换成这样就ok了呵呵

select
sum(ceiling(len(cast(SendTels as varchar(8000)))/12.00)) from
(
SELECT SendTels FROM [ZW_User_duanxin] where number=1
)
a

flashasp 2012-01-13
  • 打赏
  • 举报
回复
declare @t table (col ntext) insert into @t SELECT [SendTels] FROM [ZW_User_duanxin] where Number='1' select isnull(sum(ceiling(len(cast(col as varchar(8000)))/12.00)),0) as n from @t
这样sql语句放在asp代码里面怎么执行不了啊,nnd见鬼了今天,查询分析器里面可以的
叶子 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 flashasp 的回复:]

对了,请问楼上的兄弟 /12.00代表啥意思哦!
[/Quote]
11位电话号码+1个空格 ,因为空格有可能出现连续的两个空格,或是开始结尾有多余的1-2个空格,所以用.00控制精度 。
勿勿 2012-01-13
  • 打赏
  • 举报
回复
12为大约每个的长度 ,你也可以设置为13.00
[Quote=引用 20 楼 flashasp 的回复:]
对了,请问楼上的兄弟 /12.00代表啥意思哦!
[/Quote]
flashasp 2012-01-13
  • 打赏
  • 举报
回复
对了,请问楼上的兄弟 /12.00代表啥意思哦!
叶子 2012-01-13
  • 打赏
  • 举报
回复

declare @t table (col ntext)
insert into @t
select '10002727920 10002727926 10002727927 10002727928 ' union all
select '10002727920 10002727926 10002727927 10002727928 ' union all
select '10002727920 10002727926 10002727927 10002727928 ' union all
select '80002727926 80002727927 80002727928 11111111111 22222222222 ' union all
select '80002727920 80002727926 80002727927 80002727928' union all
select '10002727920 10002727926 10002727927 10002727928' union all
select '13609303497' union all
select '18609489876' union all
select '18609489876' union all
select '10002627920' union all
select '10002627920' union all
select '10002627920'
select
sum(ceiling(len(cast(col as varchar(8000)))/12.00)) from @t
/*
31
*/

我执行结果为31
flashasp 2012-01-13
  • 打赏
  • 举报
回复
select sum(ceiling(len(cast([sendtels] as varchar(8000)))/12.00))
from [zw_user_duanxin] where number=1
结果是29条


SELECT [SendTels] FROM [ZW_User_duanxin] where number=1
实际是31条呵呵,奇怪啊!

10002727920 10002727926 10002727927 10002727928
10002727920 10002727926 10002727927 10002727928
10002727920 10002727926 10002727927 10002727928
80002727926 80002727927 80002727928 11111111111 22222222222
80002727920 80002727926 80002727927 80002727928
10002727920 10002727926 10002727927 10002727928
13609303497
18609489876
18609489876
10002627920
10002627920
10002627920
勿勿 2012-01-13
  • 打赏
  • 举报
回复
declare @t table (col VARCHAR(60))
insert into @t
select '14002727924' union all
select '14002727924 14002727926' union all
select '14002727924 14002727926 14002727927' union all
select '84002727926 84002727927 84002727928 84002727928' union all
select '84002727924 84002727926 84002727927 84002727928' union all
select '14002727924 14002727926 14002727927 14002727928 84002727928'


;with cte as (
select B.value as value, rn=ROW_NUMBER()over(partition by value order by getdate())
from(
select
col = convert(xml,' <root> <v>' + replace(col, ' ', ' </v> <v>') + ' </v> </root>')
from @t
)A
outer apply(
select value = N.v.value('.', 'varchar(100)') from A.col.nodes('/root/v') N(v)
)B )
select value,MAX(rn)as 出现的次数 from cte group by value




value 出现的次数
---------------------------------------------------------------------------------------------------- --------------------
14002727924 4
14002727926 3
14002727927 2
14002727928 1
84002727924 1
84002727926 2
84002727927 2
84002727928 4

(8 行受影响)


叶子 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 flashasp 的回复:]

SELECT sum(len(CAST([SendTels] AS VARCHAR(8000)))-len(replace(CAST([SendTels] AS VARCHAR(8000)),' ',''))+1)
FROM [ZW_User_duanxin] 是对的,但

记录中只有一条手机号码的数据没有统计出来!,你的统计结果是25,但实际是31条
15002727920 15002……
[/Quote]

--换种思路试一试
select sum(ceiling(len(cast([sendtels] as varchar(8000)))/12.00))
from [zw_user_duanxin] where number=1
flashasp 2012-01-13
  • 打赏
  • 举报
回复
SELECT sum(len(CAST([SendTels] AS VARCHAR(8000)))-len(replace(CAST([SendTels] AS VARCHAR(8000)),' ',''))+1)
FROM [ZW_User_duanxin] 是对的,但

记录中只有一条手机号码的数据没有统计出来!,你的统计结果是25,但实际是31条
15002727920 15002727926 15002727927 15002727928
15002727920 15002727926 15002727927 15002727928
15002727920 15002727926 15002727927 15002727928
85002727920 85002727927 85002727928 11111111111 22222222222
85002727920 85002727926 85002727927 85002727928
15002727920 15002727926 15002727927 15002727928
13609303497
18609489876
18609489870
15002627920
15002627920
叶子 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 maco_wang 的回复:]

引用 12 楼 flashasp 的回复:

重复出现的号码也要统计呢,也就是说只统计电话号码出现的次数,不管重复不重复

SELECT sum(len(CAST([SendTels] AS VARCHAR(8000)))-len(replace(CAST([SendTels] AS VARCHAR(8000)),' ',''))+1)
FROM [ZW_User_duanxin]
……
[/Quote]
#9 我已经给出示例了,很明显没有过滤重复。
叶子 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 flashasp 的回复:]

重复出现的号码也要统计呢,也就是说只统计电话号码出现的次数,不管重复不重复

SELECT sum(len(CAST([SendTels] AS VARCHAR(8000)))-len(replace(CAST([SendTels] AS VARCHAR(8000)),' ',''))+1)
FROM [ZW_User_duanxin]
where number=1
是对的
但统……
[/Quote]
这行代码并没有去除重复,估计是number=1 把部分数据过滤掉了吧?
flashasp 2012-01-13
  • 打赏
  • 举报
回复
重复出现的号码也要统计呢,也就是说只统计电话号码出现的次数,不管重复不重复

SELECT sum(len(CAST([SendTels] AS VARCHAR(8000)))-len(replace(CAST([SendTels] AS VARCHAR(8000)),' ',''))+1)
FROM [ZW_User_duanxin]
where number=1
是对的
但统计出来只有14条,实际上是30个号码的
百年树人 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 josy 的回复:]
SQL code
SELECT sum(len(CAST([SendTels] AS VARCHAR(8000))-len(replace(CAST([SendTels] AS VARCHAR(8000),' ',''))+1)
FROM [ZW_User_duanxin]
where number=1
[/Quote]
SELECT sum(len(CAST([SendTels] AS VARCHAR(8000)))-len(replace(CAST([SendTels] AS VARCHAR(8000)),' ',''))+1) 
FROM [ZW_User_duanxin]
where number=1
百年树人 2012-01-13
  • 打赏
  • 举报
回复
SELECT sum(len(CAST([SendTels] AS VARCHAR(8000))-len(replace(CAST([SendTels] AS VARCHAR(8000),' ',''))+1) 
FROM [ZW_User_duanxin]
where number=1
叶子 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 flashasp 的回复:]

15002727920 15002727926 15002727927 15002727928
15002727920 15002727926 15002727927 15002727928
15002727920 15002727926 15002727927 15002727928
85002727920 85002727927 85002……
[/Quote]


--转换一下类型

declare @t table (col ntext)
insert into @t
select '14002727924' union all
select '14002727924 14002727926' union all
select '14002727924 14002727926 14002727927' union all
select '84002727926 84002727927 84002727928 84002727928' union all
select '84002727924 84002727926 84002727927 84002727928' union all
select '14002727924 14002727926 14002727927 14002727928 84002727928'

select
sum(len(cast(col as nvarchar(2000)))-
len(replace(cast(col as nvarchar(2000)),' ',''))+1) from @t
/*
19
*/

flashasp 2012-01-13
  • 打赏
  • 举报
回复
15002727920 15002727926 15002727927 15002727928
15002727920 15002727926 15002727927 15002727928
15002727920 15002727926 15002727927 15002727928
85002727920 85002727927 85002727928 11111111111 22222222222
85002727920 85002727926 85002727927 85002727928
15002727920 15002727926 15002727927 15002727928
13609303497
18609489876
18609489870
15002627920
15002627920

SELECT sum(len([SendTels] )-len(replace([SendTels] ,' ',''))+1) FROM [ZW_User_duanxin] where number=1

直接报错:)
消息 8116,级别 16,状态 1,第 1 行
参数数据类型 ntext 对于 len 函数的参数 1 无效。
flashasp 2012-01-13
  • 打赏
  • 举报
回复
不要分类,只要电话号码出现的次数即可
勿勿 2012-01-13
  • 打赏
  • 举报
回复
想要什么样的统计结果呢? 尾号分类?
加载更多回复(5)

22,210

社区成员

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

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