100分求复杂分类查询问题

morningnet 2006-04-05 10:58:43
成绩表:学号 课程号 课程性质 学分
01 k1 1 2
01 k2 1 1
01 k3 3 2
02 k1 1 2
02 k2 1 1
02 k3 3 2
02 k4 4 2
. . . .
. . . .
. . . .
表中记录有每个人所学的每一门课程的性质,课程代号和学分数
要求查询结果如下:
学号 课程性质1总学分 课程性质2总学分 课程性质3总学分 综合总学分

即按学号分类查询出每个人各类课程性质的学分和,并且对于每个人
课程性质1总学分+课程性质2总学分+课程性质3总学分+课程性质4总学分=综合总学分

???
...全文
149 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
edp08 2006-04-05
  • 打赏
  • 举报
回复
对了,总学分就用个计算字段就行了,或者在#temp表再add一个字段



sxdoujg 2006-04-05
  • 打赏
  • 举报
回复
up
iuhxq 2006-04-05
  • 打赏
  • 举报
回复
原来是交叉表,学习
iuhxq 2006-04-05
  • 打赏
  • 举报
回复
select * from table3
select id,xid,sum(f),(select sum(f) from table3 where id=a.id) from table3 a group by id, xid order by id


1 1 1 2
1 2 1 1
1 3 3 2
2 1 1 2
2 2 1 1
2 3 3 2
2 4 4 2

学号 课程性质 学分 总学分
1 1 3 5
1 3 2 5
2 1 3 7
2 3 2 7
2 4 2 7
edp08 2006-04-05
  • 打赏
  • 举报
回复
PB的话,有十字交叉表,否则:

select distinct 学号 into #temp from 成绩表

declare @li_i int, @li_max int
declare @ls_colname varchar(10)
declare @ls_update varchar(500)
select @li_max = max(课程性质) from 成绩表
set @li_i = 0
while @li_i < @Li_max
begin
set @li_i = @li_i + 1
set @ls_colname = cast(@li_i as varchar(10))
exec('alter table #temp add 课程性质' + @ls_colname + '总学分 varchar(50) ')
select @ls_update = 'update a set 课程性质' + @ls_colname + '总学分 = sum(b.学分) from #temp a, 成绩表 b where a.学号 = b.学号 and b.课程性质 = ' + @ls_colname
exec(@ls_update)
end

select * from #temp

drop table #temp

morningnet 2006-04-05
  • 打赏
  • 举报
回复
select xh, sum(xf) as zxf,t1.xz1,t2.xz2,t3.xz3,t4.xz4 from (select xh,sum(xf) as xz1 from v_cjb where kcxz='1' group by xh) t1,(select xh,sum(xf) as xz2 from v_cjb where kcxz='2' group by xh) t2,(select xh,sum(xf) as xz3 from v_cjb where kcxz='3' group by xh) t3,(select xh,sum(xf) as xz4 from cjb where kcxz='4' group by xh) t4 group by xh 这是我的解决方案,可是好像行不通
edp08 2006-04-05
  • 打赏
  • 举报
回复
你们这样写,多一个分类得加一句sql了呵,这是笨办法

我的我没试过,但方法一定管用
撸大湿 2006-04-05
  • 打赏
  • 举报
回复
declare @tb table (学号 varchar(10), 课程号 varchar(5), 课程性质 int, 学分 int)
insert @tb
select '01' ,'k1',1 ,2 union all
select '01' ,'k1',1 ,1 union all
select '01' ,'k3',3 ,2 union all
select '02' ,'k1',1 ,2 union all
select '02' ,'k2',2 ,1 union all
select '02' ,'k3',3 ,2 union all
select '02' ,'k4',4 ,2

/*学号 课程性质1总学分 课程性质2总学分 课程性质3总学分 综合总学分 */



select a.学号,
isnull((select sum(学分) from @tb where 学号=a.学号 and 课程性质=1),0)'课程性质1总学分',
isnull((select sum(学分) from @tb where 学号=a.学号 and 课程性质=2),0)'课程性质2总学分',
isnull((select sum(学分) from @tb where 学号=a.学号 and 课程性质=3),0)'课程性质3总学分',
isnull((select sum(学分) from @tb where 学号=a.学号),0)综合总学分
from @tb a group by 学号




--结果:
学号 课程性质1总学分 课程性质2总学分 课程性质3总学分 综合总学分
01 3 0 2 5
02 2 1 2 7

lsqkeke 2006-04-05
  • 打赏
  • 举报
回复
create table 成绩表(学号 varchar(10), 课程号 varchar(5), 课程性质 int, 学分 int)
insert 成绩表
select '01' , 'k1' , 1 , 2 union all
select '01' , 'k2' , 1 , 1 union all
select '01' , 'k3' , 3 , 2 union all
select '02' , 'k1' , 1 , 2 union all
select '02' , 'k2' , 2 , 1 union all
select '02' , 'k3' , 3 , 2 union all
select '02' , 'k4' , 4 , 2

/*学号 课程性质1总学分 课程性质2总学分 课程性质3总学分 综合总学分 */

declare @sql varchar(8000)
set @sql='select 学号 '
select @sql=@sql+',课程性质'+cast(课程性质 as varchar(5))+'总得分=sum(case 课程性质 when '+
cast(课程性质 as varchar(5))+' then 学分 else 0 end )' from 成绩表
group by 课程性质 order by 课程性质

set @sql=@sql+',综合总学分=sum(学分) from 成绩表 group by 学号 order by 学号'
exec(@sql)
mugua604 2006-04-05
  • 打赏
  • 举报
回复
3樓的好象做不到哦!

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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