表的数据如下:
Mail_From | Mail_To
----------------------------------------------------
liming |aa@sina.com;bb@sina.com;mmm@yahoo.com.cn
liming |aa@sina.com
liming |bb@sina.com
liming |baby@hotmail.com;mmm@yahoo.com.cn
liming |mmm@yahoo.com.cn
liming |aa@sina.com;mmm@yahoo.com.cn
我想统计出来的结果是这样
Mail_To |Num
-------------------------------
mmm@yahoo.com.cn |4
aa@sina.com |3
bb@sina.com |2
baby@hotmail.com |1
也就是统计liming都给哪些人发了邮件,按次数排倒序,最麻烦的是收件人是用";"分割开的,搞了好久没有搞出来,大家谁有什么好办法?谢谢大家100分不够再加!
16 个解决方案
#1
又
見
拆
分
不貼了,等樓下
或者直接見精華帖
見
拆
分
不貼了,等樓下
或者直接見精華帖
#2
----------------------------------------------------------------
-- Author :SQL77(只为思齐老)
-- Date :2010-03-09 15:17:54
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([Mail_From] varchar(6),[Mail_To] varchar(40))
insert #TB
select 'liming','aa@sina.com;bb@sina.com;mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com' union all
select 'liming','bb@sina.com' union all
select 'liming','baby@hotmail.com;mmm@yahoo.com.cn' union all
select 'liming','mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com;mmm@yahoo.com.cn'
--------------开始查询--------------------------
SELECT Mail_From, Mail_TO,COUNT(1) C FROM
(
select Mail_From,
SUBSTRING( Mail_To,NUMBER,CHARINDEX(';',Mail_To+';',NUMBER)-NUMBER)Mail_To
FROM #TB,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_To,NUMBER,1)=';'
)T GROUP BY
Mail_From, Mail_TO ORDER BY 3 DESC
----------------结果----------------------------
(所影响的行数为 6 行)
Mail_From Mail_TO C
--------- ---------------------------------------- -----------
liming mmm@yahoo.com.cn 4
liming aa@sina.com 3
liming bb@sina.com 2
liming baby@hotmail.com 1
(所影响的行数为 4 行)
/*
*/
#3
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([Mail_From] varchar(6),[Mail_To] varchar(40))
go
insert [tb]
select 'liming','aa@sina.com;bb@sina.com;mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com' union all
select 'liming','bb@sina.com' union all
select 'liming','baby@hotmail.com;mmm@yahoo.com.cn' union all
select 'liming','mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com;mmm@yahoo.com.cn'
select Mail_To=substring(t.Mail_To,r.number,charindex(';',t.Mail_To+';',r.number)-r.number),
count(1) as Num
from tb t,master..spt_values r
where r.number<=len(t.Mail_To) and r.type='p'
and substring(';'+t.Mail_To,r.number,1)=';'
group by substring(t.Mail_To,r.number,charindex(';',t.Mail_To+';',r.number)-r.number)
Mail_To Num
---------------------------------------- -----------
aa@sina.com 3
baby@hotmail.com 1
bb@sina.com 2
mmm@yahoo.com.cn 4
(所影响的行数为 4 行)
#4
--参考
拆分表:
--> --> (Roy)生成測試數據
if not object_id('Tab') is null
drop table Tab
Go
Create table Tab([Col1] int,[COl2] nvarchar(5))
Insert Tab
select 1,N'a,b,c' union all
select 2,N'd,e' union all
select 3,N'f'
Go
--SQL2000用辅助表:
if object_id('Tempdb..#Num') is not null
drop table #Num
go
select top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns b
Select
a.Col1,COl2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID)
from
Tab a,#Num b
where
charindex(',',','+a.Col2,b.ID)=b.ID --也可用 substring(','+a.COl2,b.ID,1)=','
--2000不使用辅助表
Select
a.Col1,COl2=substring(a.Col2,b.number,charindex(',',a.Col2+',',b.number)-b.number)
from
Tab a join master..spt_values b
ON B.type='p' AND B.number BETWEEN 1 AND LEN(A.col2)
where
substring(','+a.COl2,b.number,1)=','
SQL2005用Xml:
select
a.COl1,b.Col2
from
(select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)a
outer apply
(select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b
SQL05用CTE:
;with roy as
(select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tab
union all
select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>''
)
select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)
生成结果:
/*
Col1 COl2
----------- -----
1 a
1 b
1 c
2 d
2 e
3 f
*/
#5
create table [tb]([Mail_From] varchar(6),[Mail_To] varchar(40))
go
insert [tb]
select 'liming','aa@sina.com;bb@sina.com;mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com' union all
select 'liming','bb@sina.com' union all
select 'liming','baby@hotmail.com;mmm@yahoo.com.cn' union all
select 'liming','mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com;mmm@yahoo.com.cn'
SELECT
DISTINCT A.Mail_From,B.MAIL
FROM
(SELECT Mail_From,CONVERT(XML,'<root><row>'+REPLACE(Mail_To,';','</row><row>')+'</row></root>') AS Mail_To FROM tb)a
OUTER APPLY
(SELECT T.C.value('.','nvarchar(100)') AS MAIL FROM a.Mail_To.nodes('/root/row') T(C))b
Mail_From MAIL
--------- ---------------------------
liming aa@sina.com
liming baby@hotmail.com
liming bb@sina.com
liming mmm@yahoo.com.cn
(4 行受影响)
。。。。
#6
to SQL77
非常感谢大家
看了半天,好几个地方没有看懂~~~ TYPE='P' 是什么含义呢,还有NUMBER这个字段在master..spt_values是什么含义呢,我以前没有用过master..spt_values 这个表,我没明白按照“;”拆分的思路是怎么回事,感觉SUBSTRING( Mail_To,NUMBER,CHARINDEX(';',Mail_To+';',NUMBER)-NUMBER)中的NUMBER是关键~~~~
非常感谢大家
看了半天,好几个地方没有看懂~~~ TYPE='P' 是什么含义呢,还有NUMBER这个字段在master..spt_values是什么含义呢,我以前没有用过master..spt_values 这个表,我没明白按照“;”拆分的思路是怎么回事,感觉SUBSTRING( Mail_To,NUMBER,CHARINDEX(';',Mail_To+';',NUMBER)-NUMBER)中的NUMBER是关键~~~~
#7
master..spt_values是系统表 其实你可以直接去GOOGLE查一下 这里只是为了构造一个数字而已
#8
参考如下的内容,先拆分,然后对拆分的内容做成子查询即可.
SQL2005用Xml:
select
a.COl1,b.Col2
from
(select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)a
outer apply
(select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b
SQL05用CTE:
;with roy as
(select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tab
union all
select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>''
)
select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)
生成结果:
/*
Col1 COl2
----------- -----
1 a
1 b
1 c
2 d
2 e
3 f
*/
/*
标题:数据拆分1
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-20
地点:广东深圳
描述
有表tb, 如下:
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
欲按id,分拆value列, 分拆后结果如下:
id value
----------- --------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
*/
--1. 旧的解决方法(sql server 2000)
SELECT TOP 8000 id = IDENTITY(int, 1, 1) INTO # FROM syscolumns a, syscolumns b
SELECT A.id, value = SUBSTRING(A.[value], B.id, CHARINDEX(',', A.[value] + ',', B.id) - B.id)
FROM tb A, # B
WHERE SUBSTRING(',' + A.[value], B.id, 1) = ','
DROP TABLE #
--2. 新的解决方法(sql server 2005)
create table tb(id int,value varchar(30))
insert into tb values(1,'aa,bb')
insert into tb values(2,'aaa,bbb,ccc')
go
SELECT A.id, B.value
FROM(
SELECT id, [value] = CONVERT(xml,'<root><v>' + REPLACE([value], ',', '</v><v>') + '</v></root>') FROM tb
)A
OUTER APPLY(
SELECT value = N.v.value('.', 'varchar(100)') FROM A.[value].nodes('/root/v') N(v)
)B
DROP TABLE tb
/*
id value
----------- ------------------------------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
(5 行受影响)
*/
SQL2005用Xml:
select
a.COl1,b.Col2
from
(select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)a
outer apply
(select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b
SQL05用CTE:
;with roy as
(select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tab
union all
select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>''
)
select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)
生成结果:
/*
Col1 COl2
----------- -----
1 a
1 b
1 c
2 d
2 e
3 f
*/
#9
type='p'代表的是表的类型
#10
type='P'主要是为了去除重复NUMBER,你可以去掉type=‘P’查询看看,number有很多重复的
#11
MARK ,学习学习。。
#12
头彻底大了~~~~~~,我把上面的SQL语句一点点拆开分析了一下,明白了一些,如果说数据库里还有别的字段,比如抄送cc,密件抄送Bcc,里面的存储的地址相同的话同样要算到里面应该怎么改造呢?
比如表如下:
Mail_From | Mail_To |Mail_cc | Mail_Bcc |
---------------------------------------------------|-----------|----------|
liming |aa@sina.com;bb@sina.com;mmm@yahoo.com.cn|x@x.x;a@a.a| ..... |
liming |aa@sina.com |x@x.x | ..... |
liming |bb@sina.com |x@x.x | ..... |
liming |baby@hotmail.com;mmm@yahoo.com.cn |x@x.x | ..... |
liming |mmm@yahoo.com.cn |x@x.x | ..... |
liming |aa@sina.com;mmm@yahoo.com.cn |x@x.x | ..... |
另外大家能推荐推荐能学这些类似的SQL Server的书么,谢谢大家!!!
比如表如下:
Mail_From | Mail_To |Mail_cc | Mail_Bcc |
---------------------------------------------------|-----------|----------|
liming |aa@sina.com;bb@sina.com;mmm@yahoo.com.cn|x@x.x;a@a.a| ..... |
liming |aa@sina.com |x@x.x | ..... |
liming |bb@sina.com |x@x.x | ..... |
liming |baby@hotmail.com;mmm@yahoo.com.cn |x@x.x | ..... |
liming |mmm@yahoo.com.cn |x@x.x | ..... |
liming |aa@sina.com;mmm@yahoo.com.cn |x@x.x | ..... |
另外大家能推荐推荐能学这些类似的SQL Server的书么,谢谢大家!!!
#13
那就一个一个字段拆分,然后union all 起来,再统计.
#14
多谢dawugui提醒:
我改成了这样,出来了,但是速度有些慢了。。。。
SELECT Mail_From, Mail_TO,COUNT(1) C FROM
(
select Mail_From,
SUBSTRING( Mail_To,NUMBER,CHARINDEX(';',Mail_To+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_To,NUMBER,1)=';'
union all
select Mail_From,
SUBSTRING( Mail_cc,NUMBER,CHARINDEX(';',Mail_cc+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_cc,NUMBER,1)=';'
union all
select Mail_From,
SUBSTRING( Mail_Bcc,NUMBER,CHARINDEX(';',Mail_Bcc+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_Bcc,NUMBER,1)=';'
)T GROUP BY
Mail_From, Mail_TO ORDER BY c DESC
我改成了这样,出来了,但是速度有些慢了。。。。
SELECT Mail_From, Mail_TO,COUNT(1) C FROM
(
select Mail_From,
SUBSTRING( Mail_To,NUMBER,CHARINDEX(';',Mail_To+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_To,NUMBER,1)=';'
union all
select Mail_From,
SUBSTRING( Mail_cc,NUMBER,CHARINDEX(';',Mail_cc+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_cc,NUMBER,1)=';'
union all
select Mail_From,
SUBSTRING( Mail_Bcc,NUMBER,CHARINDEX(';',Mail_Bcc+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_Bcc,NUMBER,1)=';'
)T GROUP BY
Mail_From, Mail_TO ORDER BY c DESC
#15
该说的楼上都。。。都说了
谢谢#2的测试数据 我用函数试一下
谢谢#2的测试数据 我用函数试一下
--合并表里的Mail_To、Mail_cc、Mail_bcc三列里的邮件地址字符串 @id 为指定用户名 如 liming
CREATE FUNCTION dbo.F_MateRow(@id varchar(20))
returns varchar(8000)
AS
BEGIN
declare @str varchar(8000)
select @str=isnull(@str+';','')+Mail_To from TB where Mail_From=@id
select @str=isnull(@str+';','')+Mail_cc from TB where Mail_From=@id
select @str=isnull(@str+';','')+Mail_bcc from TB where Mail_From=@id
return @str
END
--分割字符串
CREATE FUNCTION dbo.fun_get_SplitStr(@SourceSql varchar(8000),@StrSeprate varchar(100))
returns @temp table(F1 varchar(100))
AS
BEGIN
declare @ch as varchar(100)
set @SourceSql =@SourceSql+@StrSeprate
while(@SourceSql<>'')
BEGIN
set @ch=left(@SourceSql,charindex(@StrSeprate,@SourceSql,1)-1)
insert @temp values(@ch)
set @SourceSql=stuff(@SourceSql,1,charindex(@StrSeprate,@SourceSql,1),'')
END
return
END
---查询
declare @str varchar(8000)
select @str=dbo.F_MateRow('liming')
select F1 as MailAdress,count(1) as Tot from dbo.fun_get_SplitStr(@str,';') group by F1
/*结果
MailAdress Tot
---------------------------
aa@sina.com 5
baby@hotmail.com 7
bb@sina.com 6
mmm@yahoo.com.cn 10
*/
#16
诸位大户用得弄点时间琢磨下 学习
嗯 太长的话估计8000不够。。。。
楼主看着办吧
嗯 太长的话估计8000不够。。。。
楼主看着办吧
#1
又
見
拆
分
不貼了,等樓下
或者直接見精華帖
見
拆
分
不貼了,等樓下
或者直接見精華帖
#2
----------------------------------------------------------------
-- Author :SQL77(只为思齐老)
-- Date :2010-03-09 15:17:54
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([Mail_From] varchar(6),[Mail_To] varchar(40))
insert #TB
select 'liming','aa@sina.com;bb@sina.com;mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com' union all
select 'liming','bb@sina.com' union all
select 'liming','baby@hotmail.com;mmm@yahoo.com.cn' union all
select 'liming','mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com;mmm@yahoo.com.cn'
--------------开始查询--------------------------
SELECT Mail_From, Mail_TO,COUNT(1) C FROM
(
select Mail_From,
SUBSTRING( Mail_To,NUMBER,CHARINDEX(';',Mail_To+';',NUMBER)-NUMBER)Mail_To
FROM #TB,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_To,NUMBER,1)=';'
)T GROUP BY
Mail_From, Mail_TO ORDER BY 3 DESC
----------------结果----------------------------
(所影响的行数为 6 行)
Mail_From Mail_TO C
--------- ---------------------------------------- -----------
liming mmm@yahoo.com.cn 4
liming aa@sina.com 3
liming bb@sina.com 2
liming baby@hotmail.com 1
(所影响的行数为 4 行)
/*
*/
#3
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([Mail_From] varchar(6),[Mail_To] varchar(40))
go
insert [tb]
select 'liming','aa@sina.com;bb@sina.com;mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com' union all
select 'liming','bb@sina.com' union all
select 'liming','baby@hotmail.com;mmm@yahoo.com.cn' union all
select 'liming','mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com;mmm@yahoo.com.cn'
select Mail_To=substring(t.Mail_To,r.number,charindex(';',t.Mail_To+';',r.number)-r.number),
count(1) as Num
from tb t,master..spt_values r
where r.number<=len(t.Mail_To) and r.type='p'
and substring(';'+t.Mail_To,r.number,1)=';'
group by substring(t.Mail_To,r.number,charindex(';',t.Mail_To+';',r.number)-r.number)
Mail_To Num
---------------------------------------- -----------
aa@sina.com 3
baby@hotmail.com 1
bb@sina.com 2
mmm@yahoo.com.cn 4
(所影响的行数为 4 行)
#4
--参考
拆分表:
--> --> (Roy)生成測試數據
if not object_id('Tab') is null
drop table Tab
Go
Create table Tab([Col1] int,[COl2] nvarchar(5))
Insert Tab
select 1,N'a,b,c' union all
select 2,N'd,e' union all
select 3,N'f'
Go
--SQL2000用辅助表:
if object_id('Tempdb..#Num') is not null
drop table #Num
go
select top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns b
Select
a.Col1,COl2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID)
from
Tab a,#Num b
where
charindex(',',','+a.Col2,b.ID)=b.ID --也可用 substring(','+a.COl2,b.ID,1)=','
--2000不使用辅助表
Select
a.Col1,COl2=substring(a.Col2,b.number,charindex(',',a.Col2+',',b.number)-b.number)
from
Tab a join master..spt_values b
ON B.type='p' AND B.number BETWEEN 1 AND LEN(A.col2)
where
substring(','+a.COl2,b.number,1)=','
SQL2005用Xml:
select
a.COl1,b.Col2
from
(select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)a
outer apply
(select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b
SQL05用CTE:
;with roy as
(select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tab
union all
select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>''
)
select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)
生成结果:
/*
Col1 COl2
----------- -----
1 a
1 b
1 c
2 d
2 e
3 f
*/
#5
create table [tb]([Mail_From] varchar(6),[Mail_To] varchar(40))
go
insert [tb]
select 'liming','aa@sina.com;bb@sina.com;mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com' union all
select 'liming','bb@sina.com' union all
select 'liming','baby@hotmail.com;mmm@yahoo.com.cn' union all
select 'liming','mmm@yahoo.com.cn' union all
select 'liming','aa@sina.com;mmm@yahoo.com.cn'
SELECT
DISTINCT A.Mail_From,B.MAIL
FROM
(SELECT Mail_From,CONVERT(XML,'<root><row>'+REPLACE(Mail_To,';','</row><row>')+'</row></root>') AS Mail_To FROM tb)a
OUTER APPLY
(SELECT T.C.value('.','nvarchar(100)') AS MAIL FROM a.Mail_To.nodes('/root/row') T(C))b
Mail_From MAIL
--------- ---------------------------
liming aa@sina.com
liming baby@hotmail.com
liming bb@sina.com
liming mmm@yahoo.com.cn
(4 行受影响)
。。。。
#6
to SQL77
非常感谢大家
看了半天,好几个地方没有看懂~~~ TYPE='P' 是什么含义呢,还有NUMBER这个字段在master..spt_values是什么含义呢,我以前没有用过master..spt_values 这个表,我没明白按照“;”拆分的思路是怎么回事,感觉SUBSTRING( Mail_To,NUMBER,CHARINDEX(';',Mail_To+';',NUMBER)-NUMBER)中的NUMBER是关键~~~~
非常感谢大家
看了半天,好几个地方没有看懂~~~ TYPE='P' 是什么含义呢,还有NUMBER这个字段在master..spt_values是什么含义呢,我以前没有用过master..spt_values 这个表,我没明白按照“;”拆分的思路是怎么回事,感觉SUBSTRING( Mail_To,NUMBER,CHARINDEX(';',Mail_To+';',NUMBER)-NUMBER)中的NUMBER是关键~~~~
#7
master..spt_values是系统表 其实你可以直接去GOOGLE查一下 这里只是为了构造一个数字而已
#8
参考如下的内容,先拆分,然后对拆分的内容做成子查询即可.
SQL2005用Xml:
select
a.COl1,b.Col2
from
(select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)a
outer apply
(select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b
SQL05用CTE:
;with roy as
(select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tab
union all
select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>''
)
select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)
生成结果:
/*
Col1 COl2
----------- -----
1 a
1 b
1 c
2 d
2 e
3 f
*/
/*
标题:数据拆分1
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-20
地点:广东深圳
描述
有表tb, 如下:
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
欲按id,分拆value列, 分拆后结果如下:
id value
----------- --------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
*/
--1. 旧的解决方法(sql server 2000)
SELECT TOP 8000 id = IDENTITY(int, 1, 1) INTO # FROM syscolumns a, syscolumns b
SELECT A.id, value = SUBSTRING(A.[value], B.id, CHARINDEX(',', A.[value] + ',', B.id) - B.id)
FROM tb A, # B
WHERE SUBSTRING(',' + A.[value], B.id, 1) = ','
DROP TABLE #
--2. 新的解决方法(sql server 2005)
create table tb(id int,value varchar(30))
insert into tb values(1,'aa,bb')
insert into tb values(2,'aaa,bbb,ccc')
go
SELECT A.id, B.value
FROM(
SELECT id, [value] = CONVERT(xml,'<root><v>' + REPLACE([value], ',', '</v><v>') + '</v></root>') FROM tb
)A
OUTER APPLY(
SELECT value = N.v.value('.', 'varchar(100)') FROM A.[value].nodes('/root/v') N(v)
)B
DROP TABLE tb
/*
id value
----------- ------------------------------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
(5 行受影响)
*/
SQL2005用Xml:
select
a.COl1,b.Col2
from
(select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)a
outer apply
(select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b
SQL05用CTE:
;with roy as
(select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tab
union all
select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>''
)
select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)
生成结果:
/*
Col1 COl2
----------- -----
1 a
1 b
1 c
2 d
2 e
3 f
*/
#9
type='p'代表的是表的类型
#10
type='P'主要是为了去除重复NUMBER,你可以去掉type=‘P’查询看看,number有很多重复的
#11
MARK ,学习学习。。
#12
头彻底大了~~~~~~,我把上面的SQL语句一点点拆开分析了一下,明白了一些,如果说数据库里还有别的字段,比如抄送cc,密件抄送Bcc,里面的存储的地址相同的话同样要算到里面应该怎么改造呢?
比如表如下:
Mail_From | Mail_To |Mail_cc | Mail_Bcc |
---------------------------------------------------|-----------|----------|
liming |aa@sina.com;bb@sina.com;mmm@yahoo.com.cn|x@x.x;a@a.a| ..... |
liming |aa@sina.com |x@x.x | ..... |
liming |bb@sina.com |x@x.x | ..... |
liming |baby@hotmail.com;mmm@yahoo.com.cn |x@x.x | ..... |
liming |mmm@yahoo.com.cn |x@x.x | ..... |
liming |aa@sina.com;mmm@yahoo.com.cn |x@x.x | ..... |
另外大家能推荐推荐能学这些类似的SQL Server的书么,谢谢大家!!!
比如表如下:
Mail_From | Mail_To |Mail_cc | Mail_Bcc |
---------------------------------------------------|-----------|----------|
liming |aa@sina.com;bb@sina.com;mmm@yahoo.com.cn|x@x.x;a@a.a| ..... |
liming |aa@sina.com |x@x.x | ..... |
liming |bb@sina.com |x@x.x | ..... |
liming |baby@hotmail.com;mmm@yahoo.com.cn |x@x.x | ..... |
liming |mmm@yahoo.com.cn |x@x.x | ..... |
liming |aa@sina.com;mmm@yahoo.com.cn |x@x.x | ..... |
另外大家能推荐推荐能学这些类似的SQL Server的书么,谢谢大家!!!
#13
那就一个一个字段拆分,然后union all 起来,再统计.
#14
多谢dawugui提醒:
我改成了这样,出来了,但是速度有些慢了。。。。
SELECT Mail_From, Mail_TO,COUNT(1) C FROM
(
select Mail_From,
SUBSTRING( Mail_To,NUMBER,CHARINDEX(';',Mail_To+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_To,NUMBER,1)=';'
union all
select Mail_From,
SUBSTRING( Mail_cc,NUMBER,CHARINDEX(';',Mail_cc+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_cc,NUMBER,1)=';'
union all
select Mail_From,
SUBSTRING( Mail_Bcc,NUMBER,CHARINDEX(';',Mail_Bcc+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_Bcc,NUMBER,1)=';'
)T GROUP BY
Mail_From, Mail_TO ORDER BY c DESC
我改成了这样,出来了,但是速度有些慢了。。。。
SELECT Mail_From, Mail_TO,COUNT(1) C FROM
(
select Mail_From,
SUBSTRING( Mail_To,NUMBER,CHARINDEX(';',Mail_To+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_To,NUMBER,1)=';'
union all
select Mail_From,
SUBSTRING( Mail_cc,NUMBER,CHARINDEX(';',Mail_cc+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_cc,NUMBER,1)=';'
union all
select Mail_From,
SUBSTRING( Mail_Bcc,NUMBER,CHARINDEX(';',Mail_Bcc+';',NUMBER)-NUMBER) Mail_To
FROM sWF_Mail,MASTER..SPT_VALUES
WHERE TYPE='P' AND SUBSTRING(';'+Mail_Bcc,NUMBER,1)=';'
)T GROUP BY
Mail_From, Mail_TO ORDER BY c DESC
#15
该说的楼上都。。。都说了
谢谢#2的测试数据 我用函数试一下
谢谢#2的测试数据 我用函数试一下
--合并表里的Mail_To、Mail_cc、Mail_bcc三列里的邮件地址字符串 @id 为指定用户名 如 liming
CREATE FUNCTION dbo.F_MateRow(@id varchar(20))
returns varchar(8000)
AS
BEGIN
declare @str varchar(8000)
select @str=isnull(@str+';','')+Mail_To from TB where Mail_From=@id
select @str=isnull(@str+';','')+Mail_cc from TB where Mail_From=@id
select @str=isnull(@str+';','')+Mail_bcc from TB where Mail_From=@id
return @str
END
--分割字符串
CREATE FUNCTION dbo.fun_get_SplitStr(@SourceSql varchar(8000),@StrSeprate varchar(100))
returns @temp table(F1 varchar(100))
AS
BEGIN
declare @ch as varchar(100)
set @SourceSql =@SourceSql+@StrSeprate
while(@SourceSql<>'')
BEGIN
set @ch=left(@SourceSql,charindex(@StrSeprate,@SourceSql,1)-1)
insert @temp values(@ch)
set @SourceSql=stuff(@SourceSql,1,charindex(@StrSeprate,@SourceSql,1),'')
END
return
END
---查询
declare @str varchar(8000)
select @str=dbo.F_MateRow('liming')
select F1 as MailAdress,count(1) as Tot from dbo.fun_get_SplitStr(@str,';') group by F1
/*结果
MailAdress Tot
---------------------------
aa@sina.com 5
baby@hotmail.com 7
bb@sina.com 6
mmm@yahoo.com.cn 10
*/
#16
诸位大户用得弄点时间琢磨下 学习
嗯 太长的话估计8000不够。。。。
楼主看着办吧
嗯 太长的话估计8000不够。。。。
楼主看着办吧