另一类数据汇总,有兴趣的进来做做,小弟在此谢谢了(50分)

xhunanpp 2004-05-13 03:22:54
月份 人名 课1 课2 课3 课4 课5
1 a1 A1 A2 A3 null null
1 a2 null A2 null null null
1 a3 null A2 null null null
1 a4 A1 A2 A3 null null
1 a5 A2 A5 null null null
1 a6 A3,A2 A4,A1 null A1 null
1 a7 A2,A3 A1,A4 null A1 null
1 a8 null A5 null null null
1 a9 A2 A4 null null A1
1 a10 A4 A4 null null A1
1 a11 A3,A2 A4,A1 null A1 null
1 a12 null null null null null
2 a13 A1 A2 A3 null null
2 a14 null A2 null null null
2 a15 null A2 null null null
2 a16 A1 A2 A3 null null
2 a17 A2 A5 null null null
2 a18 A3,A2 A4,A1 null A1 null
2 a19 A2,A3 A1,A4 null A1 null
2 a20 null A5 null null null
2 a21 A2 A4 null null A1
2 a22 A4 A4 null null A1
2 a23 A3,A2 A4,A1 null A1 null
2 a24 null null null null null
-------------------------------------------------------------------
汇总:
1。 得出多少条数据, (这个不用答了,)
2。 得出课1到课5有多少种选择方法,
(注意,上面有些字段用[,]分开的, 如[A1,A4]与[A4,A1] 是相同的喔,)
3。 得出课1到课5选择最多的哪一条,是哪些人选择的,

---------------------
高手们, 有谁能给我做出来吗, 有点难, 我不想用代码来做,想在SQL中直接解决, 谢谢了, (分不多了,不然就给100分)
...全文
28 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-05-13
  • 打赏
  • 举报
回复
--3。 得出课1到课5选择最多的哪一条,是哪些人选择的,

select a.*
from 表 a join(
select 月份,选课数
=case when 课1 is null then 0 else 1 end
+case when 课2 is null then 0 else 1 end
+case when 课3 is null then 0 else 1 end
+case when 课4 is null then 0 else 1 end
+case when 课5 is null then 0 else 1 end
from 表
group by 月份
)b on a.月份=b.月份 and b.选课数
=case when a.课1 is null then 0 else 1 end
+case when a.课2 is null then 0 else 1 end
+case when a.课3 is null then 0 else 1 end
+case when a.课4 is null then 0 else 1 end
+case when a.课5 is null then 0 else 1 end
zjcxc 元老 2004-05-13
  • 打赏
  • 举报
回复
--2。 得出课1到课5有多少种选择方法

--创建一个处理函数

--为了效率,所以要一个辅助表配合
select top 8000 id=identity(int,1,1) into 序数表
from syscolumns a,syscolumns b
alter table 序数表 add constraint pk_id_序数表 primary key(id)
go

--处理函数
create function f_sort(
@str varchar(8000) --要排序的字符串
)returns varchar(8000)
as
begin
declare @re varchar(8000)
set @re=''
select @re=@re+','+st
from(
select top 100 percent st=substring(@str,id,charindex(',',@str+',',id)-id)
from 序数表
where id<=len(@str)+1 and charindex(',',','+@str,id)-id=0
)a order by st
return(substring(@re,2,8000))
end
go

--调用这个函数实现你的统计
select 月份
,课1=count(distinct case when 课1 is null then null else dbo.f_sort(课1) end)
,课2=count(distinct case when 课2 is null then null else dbo.f_sort(课2) end)
,课3=count(distinct case when 课3 is null then null else dbo.f_sort(课3) end)
,课4=count(distinct case when 课4 is null then null else dbo.f_sort(课4) end)
,课5=count(distinct case when 课5 is null then null else dbo.f_sort(课5) end)
from 表
group by 月份
xhunanpp 2004-05-13
  • 打赏
  • 举报
回复
以上是按月份来分组进行汇总的喔,

先是1 月份, 再是2月份``,

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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