日期:2014-05-18  浏览次数:20397 次

一个字段用“、”分割,怎么统计?详细请进。。。
我有个数据表people   有个字段(专业)spe,
数据如下:
name     spe
张明     化学
张强     化学、英语
李东     数学、物理、生物
田雷     英语
能不能用sql实现   每个专业的人数
如:化学:2人
        英语:2人
        物理:1人
        生物:1人

基于sqlserver


------解决方案--------------------
set nocount on
declare @a table(name varchar(10), spe varchar(100))
insert @a select '张明 ', '化学 '
union all select '张强 ', '化学、英语 '
union all select '李东 ', '数学、物理、生物 '
union all select '田雷 ', '英语 '

select top 100 id=identity(int,1,1),convert(varchar(20), ' ') as a into #t from sysobjects

select 课程,count(1) 人数 from(select
substring(spe,id,charindex( '、 ',spe+ '、 ',id)-id) 课程
from @a c,#t b
where substring( '、 '+spe,id,1)= '、 ')aa group by 课程

drop table #t

--result
课程 人数
------------------------------- -----------
化学 2
生物 1
数学 1
物理 1
英语 2