我要做个电子邮件发送统计
表的数据如下:
Mail_From | Mail_To
----------------------------------------------------
liming [email protected];[email protected];[email protected]
liming [email protected]
liming [email protected]
liming [email protected];[email protected]
liming [email protected]
liming [email protected];[email protected]
我想统计出来的结果是这样
Mail_To |Num
-------------------------------
[email protected] |4
[email protected] |3
[email protected] |2
[email protected] |1
也就是统计liming都给哪些人发了邮件,按次数排倒序,最麻烦的是收件人是用";"分割开的,搞了好久没有搞出来,大家谁有什么好办法?谢谢大家100分不够再加!
------解决方案--------------------
- SQL code
------------------------------ 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)--------------------------------> 测试数据:#TBif object_id('tempdb.dbo.#TB') is not null drop table #TBgo create table #TB([Mail_From] varchar(6),[Mail_To] varchar(40))insert #TBselect 'liming',[email protected];[email protected];[email protected]' union allselect 'liming',[email protected]' union allselect 'liming',[email protected]' union allselect 'liming',[email protected];[email protected]' union allselect 'liming',[email protected]' union allselect 'liming',[email protected];[email protected]'--------------开始查询--------------------------SELECT Mail_From, Mail_TO,COUNT(1) C FROM (select Mail_From,SUBSTRING( Mail_To,NUMBER,CHARINDEX(';',Mail_To+';',NUMBER)-NUMBER)Mail_ToFROM #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 [email protected] 4liming [email protected] 3liming [email protected] 2liming [email protected] 1(所影响的行数为 4 行)/* */
------解决方案--------------------
- SQL code
--> 测试数据:[tb]if object_id('[tb]') is not null drop table [tb]create table [tb]([Mail_From] varchar(6),[Mail_To] varchar(40))goinsert [tb]select 'liming',[email protected];[email protected];[email protected]' union allselect 'liming',[email protected]' union allselect 'liming',[email protected]' union allselect 'liming',[email protected];[email protected]' union allselect 'liming',[email protected]' union allselect 'liming',[email protected];[email protected]'select Mail_To=substring(t.Mail_To,r.number,charindex(';',t.Mail_To+';',r.number)-r.number),count(1) as Numfrom tb t,master..spt_values rwhere 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 ---------------------------------------- ----------- [email protected] 3[email protected] 1[email protected] 2[email protected] 4(所影响的行数为 4 行)
------解决方案--------------------
- SQL code
--参考拆分表:--> --> (Roy)生成測試數據 if not object_id('Tab') is null drop table TabGoCreate table Tab([Col1] int,[COl2] nvarchar(5))Insert Tabselect 1,N'a,b,c' union allselect 2,N'd,e' union allselect 3,N'f'Go--SQL2000用辅助表:if object_id('Tempdb..#Num') is not null drop table #Numgoselect top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns bSelect a.Col1,COl2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID) from Tab a,#Num bwhere 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.Col2from (select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)aouter apply (select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))bSQL05用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 Tabunion allselect 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 a1 b1 c2 d2 e3 f*/