有一个成绩表
Create table Score
(
student_id varchar(10), ---学号
grade varchar(10), ---年级
subjects varchar(20), ----科目
score int ----分数
);--
表中的数据如下:
Insert into Score
Select '0001', '1', '语文', 70 from dual union
Select '0001', '1', '数学', 90 from dual union
Select '0001', '1', '英语', 89 from dual union
Select '0002', '1', '语文', 70 from dual union
Select '0002', '1', '数学', 93 from dual union
Select '0002', '1', '英语', 78 from dual union
Select '0003', '1', '语文', 80 from dual union
Select '0003', '1', '数学', 90 from dual union
Select '0003', '1', '英语', 83 from dual --希望能得到如下结果
/*
student_id grade 数学 英语 语文
0001 1 90 89 70
0002 1 93 78 70
0003 1 90 83 80
*/注:要求科目不固定的写法!!
Create table Score
(
student_id varchar(10), ---学号
grade varchar(10), ---年级
subjects varchar(20), ----科目
score int ----分数
);--
表中的数据如下:
Insert into Score
Select '0001', '1', '语文', 70 from dual union
Select '0001', '1', '数学', 90 from dual union
Select '0001', '1', '英语', 89 from dual union
Select '0002', '1', '语文', 70 from dual union
Select '0002', '1', '数学', 93 from dual union
Select '0002', '1', '英语', 78 from dual union
Select '0003', '1', '语文', 80 from dual union
Select '0003', '1', '数学', 90 from dual union
Select '0003', '1', '英语', 83 from dual --希望能得到如下结果
/*
student_id grade 数学 英语 语文
0001 1 90 89 70
0002 1 93 78 70
0003 1 90 83 80
*/注:要求科目不固定的写法!!
create table tb(姓名 nvarchar(10) , ?程 nvarchar(10) , 分数 int)
insert into tb values(N'?三' , N'?文' , 74)
insert into tb values(N'?三' , N'数学' , 83)
insert into tb values(N'?三' , N'物理' , 93)
insert into tb values(N'李四' , N'?文' , 74)
insert into tb values(N'李四' , N'数学' , 84)
insert into tb values(N'李四' , N'物理' , 94)
go--SQL SERVER 2000 静?SQL,指?程只有?文、数学、物理?三??程。(以下同)
select 姓名 as 姓名 ,
max(case ?程 when N'?文' then 分数 else 0 end) ?文,
max(case ?程 when N'数学' then 分数 else 0 end) 数学,
max(case ?程 when N'物理' then 分数 else 0 end) 物理
from tb
group by 姓名--SQL SERVER 2000 ??SQL,指?程不止?文、数学、物理?三??程。(以下同)
declare @sql varchar(8000)
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case ?程 when ''' + ?程 + ''' then 分数 else 0
end) [' + ?程 + ']'
from (select distinct ?程 from tb) as a
set @sql = @sql + N' from tb group by 姓名'SELECT CONVERT(nvarchar, @sql)
exec(@sql)--SQL SERVER 2005 静?SQL。
select * from (select * from tb) a pivot (max(分数) for ?程 in (?文,数学,物
理)) b--SQL SERVER 2005 ??SQL。
declare @sql nvarchar(4000)
select @sql = isnull(@sql + '],[' , '') + ?程 from tb group by ?程
set @sql = '[' + @sql + ']'
exec ('select * from (select * from tb) a pivot (max(分数) for ?程 in (' +
@sql + ')) b')
SELECT * FROM tbSELECT ?程 FROM tb
GROUP BY ?程---
Create table Score
(
student_id varchar(10), ---学号
grade varchar(10), ---年级
subjects varchar(20), ----科目
score int ----分数
); --
表中的数据如下:
Insert into Score
Select '0001', '1', '语文', 70 union all
Select '0001', '1', '数学', 90 union all
Select '0001', '1', '英语', 89 union all
Select '0002', '1', '语文', 70 union all
Select '0002', '1', '数学', 93 union all
Select '0002', '1', '英语', 78 union all
Select '0003', '1', '语文', 80 union all
Select '0003', '1', '数学', 90 union all
Select '0003', '1', '英语', 83
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([subjects])+'=max(case when [subjects]='+quotename([subjects],'''')+' then [score] else 0 end)'
from Score group by[subjects]
exec('select [student_id],grade'+@s+' from Score group by [student_id],grade')/*
student_id grade 数学 英语 语文
---------- ---------- ----------- ----------- -----------
0001 1 90 89 70
0002 1 93 78 70
0003 1 90 83 80
*/
declare @sql varchar(8000)
set @sql='select student_id,grade'
select @sql=@sql+',['+subjects+']=max(case subjects when '''+subjects+''' then score else 0 end)'
from (select distinct subjects from score)a
set @sql=@sql+' from score group by student_id,grade'
exec(@sql)
set @sql='select student_id,grade'
select @sql=@sql+',['+subjects+']=max(case subjects when '''+subjects+''' then score else 0 end)'
from (select distinct subjects from score)a
set @sql=@sql+' from score group by student_id,grade'
exec(@sql)
Create table Score
(
student_id varchar(10), ---学号
grade varchar(10), ---年级
subjects varchar(20), ----科目
score int ----分数
); Insert into Score
Select '0001', '1', '语文', 70 union all
Select '0001', '1', '数学', 90 union all
Select '0001', '1', '英语', 89 union all
Select '0002', '1', '语文', 70 union all
Select '0002', '1', '数学', 93 union all
Select '0002', '1', '英语', 78 union all
Select '0003', '1', '语文', 80 union all
Select '0003', '1', '数学', 90 union all
Select '0003', '1', '英语', 83 select * from Score
select Student_id,
grade,
数学= MAX(case when subjects='数学' then Score else 0 end),
英语=MAX(case when subjects='英语' then Score else 0 end),
语文=MAX(case when subjects='语文' then Score else 0 end)
from Score a
group by student_id ,grade
/*
student_id grade 数学 英语 语文
0001 1 90 89 70
0002 1 93 78 70
0003 1 90 83 80
*/
记得有个函数decode可以部分帮到你