SQLServer差费逗号分隔的字段并统计 10
有个表中的一个字段Author,如下IDAuthor1张三2张三,李四3王五4李四5张三,李四,王五现在想查询出这样的结果AuthorCount张三3李四3王五2求高手啊...
有个表中的一个字段Author,如下
ID Author
1 张三
2 张三,李四
3 王五
4 李四
5 张三,李四,王五
现在想查询出这样的结果
Author Count
张三 3
李四 3
王五 2
求高手啊,实在没有分了 展开
ID Author
1 张三
2 张三,李四
3 王五
4 李四
5 张三,李四,王五
现在想查询出这样的结果
Author Count
张三 3
李四 3
王五 2
求高手啊,实在没有分了 展开
2个回答
展开全部
create table author(
id int identity,
author nvarchar(20)
)
insert into author values('张三')
insert into author values('张三,李四')
insert into author values('王五')
insert into author values('李四')
insert into author values('张三,李四,王五')
select * from author
select OneTitle,COUNT(OneTitle) as count
from
(
select id,
Author=convert(xml, '<root><v>' + replace(Author,',','</v><v>') + '</v></root>')
from Author
)a
outer apply
(
select OneTitle = C.v.value('.', 'nvarchar(100)')
from a.Author.nodes('/root/v')C(v)
) b
group by OneTitle order by count desc
id int identity,
author nvarchar(20)
)
insert into author values('张三')
insert into author values('张三,李四')
insert into author values('王五')
insert into author values('李四')
insert into author values('张三,李四,王五')
select * from author
select OneTitle,COUNT(OneTitle) as count
from
(
select id,
Author=convert(xml, '<root><v>' + replace(Author,',','</v><v>') + '</v></root>')
from Author
)a
outer apply
(
select OneTitle = C.v.value('.', 'nvarchar(100)')
from a.Author.nodes('/root/v')C(v)
) b
group by OneTitle order by count desc
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if object_id('Tempdb..#Num') is not null
drop table #Num
select top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns b
Select
Author=substring(a.Author,b.ID,charindex(',',a.Author+',',b.ID)-b.ID),count(*)
from
table1 a,#Num b
where
charindex(',',','+a.Author,b.ID)=b.ID
group by substring(a.Author,b.ID,charindex(',',a.Author+',',b.ID)-b.ID);
table1替换成你自己表名,top 100 那个100你就替换个稍微大点的数吧
drop table #Num
select top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns b
Select
Author=substring(a.Author,b.ID,charindex(',',a.Author+',',b.ID)-b.ID),count(*)
from
table1 a,#Num b
where
charindex(',',','+a.Author,b.ID)=b.ID
group by substring(a.Author,b.ID,charindex(',',a.Author+',',b.ID)-b.ID);
table1替换成你自己表名,top 100 那个100你就替换个稍微大点的数吧
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询