补充:学科即subjectid放在一个数组里面,是可以知道的;但是成绩里不一定都有该学科
解决方案 »
- 关于数据库表结构的更新
- 插入内容,有主键,却显示键列信息不足或不正确,求教~~
- 一句sql语句,不知道应该建立索引还是视图还是别的什么方式优化
- =====一道题目,导txt文件入数据库问题,高手帮助======
- 象这种自动编号,是如何生成的?a8efc285-f0b1-4f8f-8e73-2b7d8724a47c
- 如何修改主键得值
- 怎样把要置顶的信息和其它信息一起查询出来
- 救命,在线等。
- 有些包含ASCII码大于128的字符的字符串在执行insert时出错,如何解决
- 各位同仁在分发ORACLE程序时怎么处理ORACLE的客户端?
- 入门问题:在一台没有装SQL Server 2000的电脑上开发软件,数据库用服务器上的SQL Server. 如何做?
- 请教邹建等高手,为何MSDE安装后怎么也启动不了?谢谢
insert into t_score select 2,201002,1,90
insert into t_score select 3,201003,1,100
insert into t_score select 4,201001,2,80
insert into t_score select 5,201001,3,85
insert into t_score select 6,201002,2,86
insert into t_score select 7,201004,3,87
--需要一张subject_id与subject_name对照表t_subject
create table t_subject(subject_id int,subject_name varchar(20))insert into t_subject select 1,'语文'
insert into t_subject select 2,'数学'
insert into t_subject select 3,'物理'
insert into t_subject select 4,'化学'
declare @s varchar(8000)
set @s = 'select stuent_id'
select @s = @s +','+subject_name+'=max(case when subject_id = '+rtrim(subject_id)+' then score end)'
from t_subject order by subject_id
set @s = @s + ' from t_score group by stuent_id order by stuent_id'
exec(@s)
sum(case subject_id when 1 then score else 0 end)as 语文,
sum(case subject_id when 2 then score else 0 end)as 数学,
sum(case subject_id when 3 then score else 0 end)as 物理,
sum(case subject_id when 4 then score else 0 end)as 化学
from t
group by stuent_id order by stuent_id
insert into t_score select 1,201001,1,80
insert into t_score select 2,201002,1,90
insert into t_score select 3,201003,1,100
insert into t_score select 4,201001,2,80
insert into t_score select 5,201001,3,85
insert into t_score select 6,201002,2,86
insert into t_score select 7,201004,3,87--如果已经明确了学科id与学科名称的对应关系,用如下查询select
学号 = stuent_id,
语文 = max(case subject_id when 1 then score end),
数学 = max(case subject_id when 2 then score end),
物理 = max(case subject_id when 3 then score end),
化学 = max(case subject_id when 4 then score end)
from
t_score
group by
stuent_id
order by
stuent_id
stuent_id,
語文=max(case when subject_id = 1 then score end),
數學=max(case when subject_id = 2 then score end),
物理=max(case when subject_id = 3 then score end),
化學=max(case when subject_id = 4 then score end)
from t_score
group by stuent_id
order by stuent_id
create table t_score(id int,stuent_id int,subject_id int,score int)insert into t_score select 1,201001,1,80
insert into t_score select 2,201002,1,90
insert into t_score select 3,201003,1,100
insert into t_score select 4,201001,2,80
insert into t_score select 5,201001,3,85
insert into t_score select 6,201002,2,86
insert into t_score select 7,201004,3,87
--需要一张subject_id与subject_name对照表t_subject
create table t_subject(subject_id int,subject_name varchar(20))insert into t_subject select 1,'语文'
insert into t_subject select 2,'数学'
insert into t_subject select 3,'物理'
insert into t_subject select 4,'化学'
declare @s varchar(8000)
set @s = 'select stuent_id'
select @s = @s +','+subject_name+'=max(case when subject_id = '+rtrim(subject_id)+' then score end)'
from t_subject
--增加以下条件
where subject_id in(1,2,3,5,6)
order by subject_idset @s = @s + ' from t_score group by stuent_id order by stuent_id'
exec(@s)
insert test select '201001','语文',80
union all select '201001','化学',80
union all select '201002','语文',90
union all select '201003','语文',100
union all select '201001','数学',80
union all select '201001','物理',85
union all select '201002','数学',86
union all select '201004','物理',87
select * from testDeclare @s varchar(2000)
set @s=''
select @s=@s+','+subject_id+'=isnull((select score from test where stuent_id=a.stuent_id and subject_id='''+subject_id+'''),0)' from test group by subject_id
set @s='Select stuent_id '+@s+' from test a group by stuent_id'
print(@s)
exec(@s)
drop table test
还有一个很重要的,如何加一个求和和排名,如果能解决,万分感谢!student 语文 数学 ... 历史 总分 排名
Declare @s varchar(2000)
set @s=''select @s=@s+','+subject_id+'=isnull((select score from test where stuent_id=a.stuent_id and subject_id='''+subject_id+'''),0)' from test group by subject_id
set @s='Select stuent_id '+@s+',total=sum(score) from test a group by stuent_id order by total' exec(@s)
按总分顺序排列
sql语句写得这么熟练,很佩服你得的方法和 libin_ftsafe(子陌红尘)的有什么区别吗?能否讲解一下,谢谢
如何存储过程的一个整型字符例如1,2,3,5,6,便于我在用where subjectid in(1,2,3,5,6)中使用,我怎么定义都不行啊,总是int和varchar转换错误
zhaozhaozhaozhao:我的数据库存放subjectid是个整型,所以我做了一下改动:SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GOALTER PROCEDURE Score_Report
@subjectid int,@GradeName int
AS
Declare @s varchar(2000)set @s=''select @s=@s+','+'['+cast(subjectid as varchar)+']'+'=isnull((select score from s_c_test_item where st_barno=a.st_barno and subjectid='''+cast(subjectid as varchar)+'''),0)' from s_c_test_item where subjectid in (@subjectid) and s_c_test_id in (@GradeName) group by subjectid
set @s='Select st_barno '+@s+',total=sum(score) from s_c_test_item a group by st_barno order by total' print @s
exec(@s)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GOexec Score_Report '1, 2, 3' ,2 --这里怎么传入1,2,3啊!!!!!在where subjectid in (@subjectid)中必须要求@subjectid是个int啊!
如果仅仅是这里有问题,可以改成
select @s=@s+','+'['+cast(subjectid as varchar)+']'+'=isnull((select score from s_c_test_item where st_barno=a.st_barno and subjectid='''+cast(subjectid as varchar)+'''),0)' from s_c_test_item where subjectid in (' + @subjectid + ') and s_c_test_id in (@GradeName) group by subjectid
set @s='Select st_barno '+@s+',total=sum(score) from s_c_test_item a group by st_barno order by total'
id stuent_id subject_id score
1 201001 1 80
2 201002 1 90
3 201003 1 100
4 201001 2 80
5 201001 3 85
6 201002 2 86
7 201004 3 87
假定表名称:student
第2步:
CREATE TABLE [student1] (
[学号] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[语文] [decimal](18, 0) NULL ,
[数学] [decimal](18, 0) NULL ,
[物理] [decimal](18, 0) NULL ,
[化学] [decimal](18, 0) NULL
(或手工创建student1)第3步:
delete student1
go
INSERT INTO student1 (学号) select distinct student_id from student
go
update student1
set 语文=student.score
from student where student1.学号=student.student_id and student.subject_id=1
go
update student1
set 数学=student.score
from student where student1.学号=student.student_id and student.subject_id=2
go
update student1
set 物理=student.score
from student where student1.学号=student.student_id and student.subject_id=3
go
update student1
set 化学=student.score
from student where student1.学号=student.student_id and student.subject_id=4
go