sql字符拆分字段,并统计(在线等)

leisunzhi 2013-12-18 08:46:57
有一张表如图

需要统计成如下图

如何用sqlserver试下,sql2008,求指教。
...全文
418 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sj490790083 2013-12-18
  • 打赏
  • 举报
回复

;with cte as
(select SUBSTRING(xueke,0,charindex(',',xueke)) xueke,hits from T1218
union all select RIGHT(xueke,len(xueke)-charindex(',',xueke)) xueke,hits from T1218)
select xueke,SUM(hits) from cte where xueke<>'' group by xueke order by xueke desc
/*             xueke   hits
		语文	5
		英语	3
		物理	10
		数学	2
		生物	6
		化学	7  */
Andy__Huang 2013-12-18
  • 打赏
  • 举报
回复
master..spt_values 是sql server的系统表,这个存在基数,可以用这个表的基数协助参与计算,能够提高性能。
Andy__Huang 2013-12-18
  • 打赏
  • 举报
回复
create table #tb([xueke] varchar(50),[hits] int)
insert #tb
select '数学,语文',2 union all
select '语文,英语',3 union all
select '化学,物理',4 union all
select '物理,生物',6 union all
select '化学',3

select [xueke],SUM(hits) as hits
from
(SELECT SUBSTRING([xueke],number,CHARINDEX(',',[xueke]+',',number)-number) as [xueke],a.hits
from #tb a, master..spt_values 
where number >=1 and type='p'  
	and number<len([xueke])  and substring(','+[xueke],number,1)=','
)t
group by [xueke]
order by 2

/*
xueke	hits
数学	2
英语	3
语文	5
生物	6
化学	7
物理	10
*/
Yole 2013-12-18
  • 打赏
  • 举报
回复

--实现split功能 的函数  
create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))  
returns @temp table(a varchar(100))  
as   
begin  
declare @i int  
set @SourceSql=rtrim(ltrim(@SourceSql))  
set @i=charindex(@StrSeprate,@SourceSql)  
while @i>=1  
begin  
insert @temp values(left(@SourceSql,@i-1))  
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)  
set @i=charindex(@StrSeprate,@SourceSql)  
end  
if @SourceSql<>'\'  
insert @temp values(@SourceSql)  
return   
end  
  
go 

--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go 
create table [a]([xueke] varchar(50),[hits] int)
insert [a]
select '数学,语文',2 union all
select '语文,英语',3 union all
select '化学,物理',4 union all
select '物理,生物',6 union all
select '化学',3

if object_id('[b]') is not null drop table [b]
go 
create table [b]([xueke] varchar(50),[hits] int)


select row_number() over(order by xueke ) as rn,* into #a from a

declare @i int ,@j int ,@k varchar(10),@l int
set @i =1 
select @j=max(rn) from #a
while(@i<=@j)
begin 
select @k=[xueke],@l= [hits] from #a where rn =@i
insert into b select a,@l from  dbo.f_split(@k,',')
set @i=@i+1
end 
select xueke ,sum(hits) as hits from b group by xueke

xueke hits -------------------------------------------------- ----------- 化学 7 生物 6 数学 2 物理 10 英语 3 语文 5 (6 行受影响)
thinkingforever 2013-12-18
  • 打赏
  • 举报
回复
通过游标便利行,每行进行字符串截取,截取后写入到临时表中,最后通过临时表统计。
leisunzhi 2013-12-18
  • 打赏
  • 举报
回复
思路我知道,主要是要详细sql语句
  • 打赏
  • 举报
回复
1. SQL问题发MSSQL 板块 2、关于你的问题 首先用datatable获取到第一张表dt1的数据 然后就都是字符串操作 new一个datatable来存下就OK了

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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