学号,姓名,课程名称,成绩
01, 张三,数学, 70
01, 张三,英语, 70
01, 张三,政治, 70
02, 李四,数学, 70
02, 李四,英语, 70
02, 李四,政治, 70
请问怎样的SQL 语句能把他变成这样的格式
学号,姓名,数学,英语,政治
01 张三 70, 70 ,70
02 李四 70, 70 ,70
请各位帮忙,谢谢
6 个解决方案
#1
拜托,不要再问了好不好~~~
#2
SQL交叉表实例
在查询分析器里运行:
CREATE TABLE [Test] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Source] [numeric](18, 0) NULL
) ON [PRIMARY]
GO
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'语文',60)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'数学',70)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'英语',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'数学',75)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'语文',57)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'语文',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'英语',100)
Go
交叉表语句的实现:
--用于:交叉表的列数是确定的
select name,sum(case subject when '数学' then source else 0 end) as '数学',
sum(case subject when '英语' then source else 0 end) as '英语',
sum(case subject when '语文' then source else 0 end) as '语文'
from test
group by name
--用于:交叉表的列数是不确定的
declare @sql varchar(8000)
set @sql = 'select name,'
select @sql = @sql + 'sum(case subject when '''+subject+'''
then source else 0 end) as '''+subject+''','
from (select distinct subject from test) as a
select @sql = left(@sql,len(@sql)-1) + ' from test group by name'
exec(@sql)
go
在查询分析器里运行:
CREATE TABLE [Test] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Source] [numeric](18, 0) NULL
) ON [PRIMARY]
GO
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'语文',60)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'数学',70)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'英语',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'数学',75)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'语文',57)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'语文',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'英语',100)
Go
交叉表语句的实现:
--用于:交叉表的列数是确定的
select name,sum(case subject when '数学' then source else 0 end) as '数学',
sum(case subject when '英语' then source else 0 end) as '英语',
sum(case subject when '语文' then source else 0 end) as '语文'
from test
group by name
--用于:交叉表的列数是不确定的
declare @sql varchar(8000)
set @sql = 'select name,'
select @sql = @sql + 'sum(case subject when '''+subject+'''
then source else 0 end) as '''+subject+''','
from (select distinct subject from test) as a
select @sql = left(@sql,len(@sql)-1) + ' from test group by name'
exec(@sql)
go
#3
你能帮我解答吗,还是这个问题很浅啊.
#4
上面写的很清楚来,我给你换种写法
select distinct 学号,姓名,(select 成绩 from yourtable where 学号=A.学号 and 课程名称='数学') 数学,
(select 成绩 from yourtable where 学号=A.学号 and 课程名称='英语') 英语,
(select 成绩 from yourtable where 学号=A.学号 and 课程名称='语文') 语文
from yourtable A
或
declare @sql varchar(8000)
set @sql = 'select distinct 学号,姓名,'
select @sql = @sql + '(select 成绩 from yourtable where 学号=A.学号 and 课程名称= '''+课程名称+''') '+课程名称+','
from (select distinct 课程名称 from yourtable) as a
select @sql = left(@sql,len(@sql)-1) + ' from yourtable A'
exec(@sql)
go
select distinct 学号,姓名,(select 成绩 from yourtable where 学号=A.学号 and 课程名称='数学') 数学,
(select 成绩 from yourtable where 学号=A.学号 and 课程名称='英语') 英语,
(select 成绩 from yourtable where 学号=A.学号 and 课程名称='语文') 语文
from yourtable A
或
declare @sql varchar(8000)
set @sql = 'select distinct 学号,姓名,'
select @sql = @sql + '(select 成绩 from yourtable where 学号=A.学号 and 课程名称= '''+课程名称+''') '+课程名称+','
from (select distinct 课程名称 from yourtable) as a
select @sql = left(@sql,len(@sql)-1) + ' from yourtable A'
exec(@sql)
go
#5
行转列的问题!
#6
谢谢个位帮忙.
#1
拜托,不要再问了好不好~~~
#2
SQL交叉表实例
在查询分析器里运行:
CREATE TABLE [Test] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Source] [numeric](18, 0) NULL
) ON [PRIMARY]
GO
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'语文',60)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'数学',70)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'英语',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'数学',75)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'语文',57)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'语文',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'英语',100)
Go
交叉表语句的实现:
--用于:交叉表的列数是确定的
select name,sum(case subject when '数学' then source else 0 end) as '数学',
sum(case subject when '英语' then source else 0 end) as '英语',
sum(case subject when '语文' then source else 0 end) as '语文'
from test
group by name
--用于:交叉表的列数是不确定的
declare @sql varchar(8000)
set @sql = 'select name,'
select @sql = @sql + 'sum(case subject when '''+subject+'''
then source else 0 end) as '''+subject+''','
from (select distinct subject from test) as a
select @sql = left(@sql,len(@sql)-1) + ' from test group by name'
exec(@sql)
go
在查询分析器里运行:
CREATE TABLE [Test] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Source] [numeric](18, 0) NULL
) ON [PRIMARY]
GO
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'语文',60)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'数学',70)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'英语',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'数学',75)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'语文',57)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'语文',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'英语',100)
Go
交叉表语句的实现:
--用于:交叉表的列数是确定的
select name,sum(case subject when '数学' then source else 0 end) as '数学',
sum(case subject when '英语' then source else 0 end) as '英语',
sum(case subject when '语文' then source else 0 end) as '语文'
from test
group by name
--用于:交叉表的列数是不确定的
declare @sql varchar(8000)
set @sql = 'select name,'
select @sql = @sql + 'sum(case subject when '''+subject+'''
then source else 0 end) as '''+subject+''','
from (select distinct subject from test) as a
select @sql = left(@sql,len(@sql)-1) + ' from test group by name'
exec(@sql)
go
#3
你能帮我解答吗,还是这个问题很浅啊.
#4
上面写的很清楚来,我给你换种写法
select distinct 学号,姓名,(select 成绩 from yourtable where 学号=A.学号 and 课程名称='数学') 数学,
(select 成绩 from yourtable where 学号=A.学号 and 课程名称='英语') 英语,
(select 成绩 from yourtable where 学号=A.学号 and 课程名称='语文') 语文
from yourtable A
或
declare @sql varchar(8000)
set @sql = 'select distinct 学号,姓名,'
select @sql = @sql + '(select 成绩 from yourtable where 学号=A.学号 and 课程名称= '''+课程名称+''') '+课程名称+','
from (select distinct 课程名称 from yourtable) as a
select @sql = left(@sql,len(@sql)-1) + ' from yourtable A'
exec(@sql)
go
select distinct 学号,姓名,(select 成绩 from yourtable where 学号=A.学号 and 课程名称='数学') 数学,
(select 成绩 from yourtable where 学号=A.学号 and 课程名称='英语') 英语,
(select 成绩 from yourtable where 学号=A.学号 and 课程名称='语文') 语文
from yourtable A
或
declare @sql varchar(8000)
set @sql = 'select distinct 学号,姓名,'
select @sql = @sql + '(select 成绩 from yourtable where 学号=A.学号 and 课程名称= '''+课程名称+''') '+课程名称+','
from (select distinct 课程名称 from yourtable) as a
select @sql = left(@sql,len(@sql)-1) + ' from yourtable A'
exec(@sql)
go
#5
行转列的问题!
#6
谢谢个位帮忙.