select 姓名,语文=(select 分数 from 表B where 科目='语文' and id=表A.id),数学=(select 分数 from 表B where 科目='数学' and id=表A.id),英语=(select 分数 from 表B where 科目='英语' and id=表A.id) from 表A 助人为快乐之本!
---Lz问题应该解决了吧 ---如果科目固定只有语文、数据、英语 Select A.姓名, Max(Case B.科目 When '语文' Then B.分数 Else Null End) As 语文分数, Max(Case B.科目 When '数学' Then B.分数 Else Null End) As 数学分数, Max(Case B.科目 When '英语' Then B.分数 Else Null End) As 英语分数 From 表B B Inner Join 表A A On A.id=B.id Group By A.姓名---如果科目不固定有更多的科目 Declare @sql Varchar(8000) Set @sql='' Select @sql=@sql+',Max(Case 科目 When '''+科目+''' Then 分数 Else Null End) As ['+科目+'分数]' From 表B Group By 科目 Exec('Select A.姓名'+@sql+'From 表B B Inner Join 表A A On A.id=B.id Group By A.姓名')
create table A (id VARCHAR(10),姓名 varchar(10)) insert into A SELECT '001','陳剛' UNION ALL SELECT '002','李芳' UNION ALL SELECT '003','王明'create table B(id varchar(10),科目 varchar(10),分數 int) insert into B select '001','語文',78 union all select '002','英語',90 union all select '001','英語',88 union all select '003','數學',99 union all select '002','語文',60 union all select '001','數學',54 union all select '003','英語',23select * from b Select A.姓名, Max(Case B.科目 When '語文' Then B.分數 Else Null End) As 語文分數, Max(Case B.科目 When '數學' Then B.分數 Else Null End) As 數學分數, Max(Case B.科目 When '英語' Then B.分數 Else Null End) As 英語分數 From B B left Join A A On A.id=B.id Group By A.姓名 姓名 語文分數 數學分數 英語分數 ---------- ----------- ----------- ----------- 王明 NULL 99 23 李芳 60 NULL 90 陳剛 78 54 88(3 row(s) affected)Warning: Null value is eliminated by an aggregate or other SET operation.
declare @t table (id int,xm varchar(20)) insert into @t select 1,'zhang' union all select 2,'wang' union all select 3,'li' declare @b table (id int,km varchar(20),fs int) insert into @b select 1,'yw',80 union all select 2,'sx',90 union all select 3,'yy',90 union all select 2,'yw',90 union all select 1,'sx',90 union all select 2,'yy',90select a.xm,max(case b.km when 'yw' then b.fs else null end) as yw, max(case b.km when 'sx' then b.fs else null end) as sx, max(case b.km when 'yy' then b.fs else null end) as yy from @t a inner join @b b on a.id=b.id group by a.xm
declare @varSql varchar(1000) set @varSql='select name' select @varSql=@varSql+' ,max(case subject when subject when '''+subject+''' then score else 0 end)' from ( select subject from b group by subject )tSet @varSql=@varSql+' from (select * from a left join b on a.id=b.id) tt 'exec(@varSql)
ayzwd(菜鸟老三) 及fa_ge(鶴嘯九天) 的写法都是正确的,顶!!
ljwfbf() ( ) 信誉:100 Lz,你写的SQL语句更差,只输出了两个人的信息! 执行了你的语句后,输出如下:李芳60NULL90 王明NULL9923 /***楼主我的语句更差,有问题吗???????***/ ------------------------------------------------- ---创建测试数据 Create Table 表A(id Varchar(4),姓名 Varchar(8)) Insert 表A Select '001','陈刚' Union All Select '002','李芳' Union All Select '003','王明'Create Table 表B(id Varchar(4),科目 Varchar(8),分数 Int) Insert 表B Select '001','语文',78 Union All Select '002','英语',90 Union All Select '001','英语',88 Union All Select '003','数学',99 Union All Select '002','语文',60 Union All Select '001','数学',54 Union All Select '003','英语',23 Select * From 表A Select * From 表B ---查询结果1 Select A.姓名, Max(Case B.科目 When '语文' Then B.分数 Else Null End) As 语文分数, Max(Case B.科目 When '数学' Then B.分数 Else Null End) As 数学分数, Max(Case B.科目 When '英语' Then B.分数 Else Null End) As 英语分数 From 表B B Inner Join 表A A On A.id=B.id Group By A.姓名 ---结果 /* 姓名 语文分数 数学分数 英语分数 -------- ----------- ----------- ----------- 陈刚 78 54 88 李芳 60 NULL 90 王明 NULL 99 23(所影响的行数为 3 行)警告: 聚合或其它 SET 操作消除了空值。 */---查询结果2 Declare @sql Varchar(8000) Set @sql='' Select @sql=@sql+',Max(Case 科目 When '''+科目+''' Then 分数 Else Null End) As ['+科目+'分数]' From 表B Group By 科目 Exec('Select A.姓名'+@sql+'From 表B B Inner Join 表A A On A.id=B.id Group By A.姓名') ---结果 /* 姓名 数学分数 英语分数 语文分数 -------- ----------- ----------- ----------- 陈刚 54 88 78 李芳 NULL 90 60 王明 99 23 NULL警告: 聚合或其它 SET 操作消除了空值。 */
---如果科目固定只有语文、数据、英语
Select
A.姓名,
Max(Case B.科目 When '语文' Then B.分数 Else Null End) As 语文分数,
Max(Case B.科目 When '数学' Then B.分数 Else Null End) As 数学分数,
Max(Case B.科目 When '英语' Then B.分数 Else Null End) As 英语分数
From
表B B
Inner Join
表A A
On A.id=B.id Group By A.姓名---如果科目不固定有更多的科目
Declare @sql Varchar(8000)
Set @sql=''
Select @sql=@sql+',Max(Case 科目 When '''+科目+''' Then 分数 Else Null End) As ['+科目+'分数]'
From 表B Group By 科目
Exec('Select A.姓名'+@sql+'From 表B B Inner Join 表A A On A.id=B.id Group By A.姓名')
李芳 60 NULL 90
王明 NULL 99 23
但,输出的结果应该是:陈刚 78 54 88
李芳 60 null 90
王明 null 99 23
执行了你的语句后,输出如下:李芳 60 NULL 90
王明 NULL 99 23
create table A
(id VARCHAR(10),姓名 varchar(10))
insert into A
SELECT '001','陳剛' UNION ALL
SELECT '002','李芳' UNION ALL
SELECT '003','王明'create table B(id varchar(10),科目 varchar(10),分數 int)
insert into B
select '001','語文',78 union all
select '002','英語',90 union all
select '001','英語',88 union all
select '003','數學',99 union all
select '002','語文',60 union all
select '001','數學',54 union all
select '003','英語',23select * from b
Select
A.姓名,
Max(Case B.科目 When '語文' Then B.分數 Else Null End) As 語文分數,
Max(Case B.科目 When '數學' Then B.分數 Else Null End) As 數學分數,
Max(Case B.科目 When '英語' Then B.分數 Else Null End) As 英語分數
From
B B
left Join
A A
On A.id=B.id Group By A.姓名
姓名 語文分數 數學分數 英語分數
---------- ----------- ----------- -----------
王明 NULL 99 23
李芳 60 NULL 90
陳剛 78 54 88(3 row(s) affected)Warning: Null value is eliminated by an aggregate or other SET operation.
insert into @t
select 1,'zhang'
union all select 2,'wang'
union all select 3,'li'
declare @b table (id int,km varchar(20),fs int)
insert into @b
select 1,'yw',80
union all select 2,'sx',90
union all select 3,'yy',90
union all select 2,'yw',90
union all select 1,'sx',90
union all select 2,'yy',90select a.xm,max(case b.km when 'yw' then b.fs else null end) as yw,
max(case b.km when 'sx' then b.fs else null end) as sx,
max(case b.km when 'yy' then b.fs else null end) as yy
from @t a inner join @b b
on a.id=b.id
group by a.xm
declare @varSql varchar(1000)
set @varSql='select name'
select @varSql=@varSql+' ,max(case subject when subject when '''+subject+''' then score else 0 end)' from
( select subject from b group by subject )tSet @varSql=@varSql+' from (select * from a left join b on a.id=b.id) tt 'exec(@varSql)
Lz,你写的SQL语句更差,只输出了两个人的信息!
执行了你的语句后,输出如下:李芳60NULL90
王明NULL9923
/***楼主我的语句更差,有问题吗???????***/
-------------------------------------------------
---创建测试数据
Create Table 表A(id Varchar(4),姓名 Varchar(8))
Insert 表A Select '001','陈刚'
Union All Select '002','李芳'
Union All Select '003','王明'Create Table 表B(id Varchar(4),科目 Varchar(8),分数 Int)
Insert 表B Select '001','语文',78
Union All Select '002','英语',90
Union All Select '001','英语',88
Union All Select '003','数学',99
Union All Select '002','语文',60
Union All Select '001','数学',54
Union All Select '003','英语',23
Select * From 表A
Select * From 表B
---查询结果1
Select
A.姓名,
Max(Case B.科目 When '语文' Then B.分数 Else Null End) As 语文分数,
Max(Case B.科目 When '数学' Then B.分数 Else Null End) As 数学分数,
Max(Case B.科目 When '英语' Then B.分数 Else Null End) As 英语分数
From
表B B
Inner Join
表A A
On A.id=B.id Group By A.姓名
---结果
/*
姓名 语文分数 数学分数 英语分数
-------- ----------- ----------- -----------
陈刚 78 54 88
李芳 60 NULL 90
王明 NULL 99 23(所影响的行数为 3 行)警告: 聚合或其它 SET 操作消除了空值。
*/---查询结果2
Declare @sql Varchar(8000)
Set @sql=''
Select @sql=@sql+',Max(Case 科目 When '''+科目+''' Then 分数 Else Null End) As ['+科目+'分数]'
From 表B Group By 科目
Exec('Select A.姓名'+@sql+'From 表B B Inner Join 表A A On A.id=B.id Group By A.姓名')
---结果
/*
姓名 数学分数 英语分数 语文分数
-------- ----------- ----------- -----------
陈刚 54 88 78
李芳 NULL 90 60
王明 99 23 NULL警告: 聚合或其它 SET 操作消除了空值。
*/