这个可以简化一下,不用分组:
----创建测试数据
declare @studentmain table(student_id varchar(10),student_name varchar(10))
insert @studentmain
select '00001', 'Mary' union all
select '00002', 'Jack'
declare @studentscore table(student_id varchar(10),test_id int,student_score int)
insert @studentscore
select '00001', 1001, 56 union all
select '00001', 1002, 78 union all
select '00001', 1003, 98
declare @subjectmain table(subject_id int,subject_name varchar(10))
insert @subjectmain
select 11111, 'Maths' union all
select 22222, 'Chinese'
declare @testschedule table(test_id int,subject_id int,test_date varchar(10))
insert @testschedule
select 1001, 11111, '2007/1/1' union all
select 1002, 11111, '2007/2/2' union all
select 1003, 22222, '2007/3/3'
----查询
SELECT
a.student_name,
d.subject_name,
e.student_score
FROM @studentmain AS a
CROSS JOIN (select * from @testschedule as t where t.test_date =
(select max(test_date) from @testschedule where subject_id = t.subject_id)) AS c
LEFT JOIN @subjectmain AS d ON c.subject_id = d.subject_id
LEFT JOIN @studentscore AS e ON c.test_id = e.test_id and a.student_id = e.student_id
ORDER BY a.student_name DESC,student_score
/*结果:
student_name subject_name student_score
------------ ------------ -------------
Mary Maths 78
Mary Chinese 98
Jack Maths NULL
Jack Chinese NULL
*/
----创建测试数据
declare @studentmain table(student_id varchar(10),student_name varchar(10))
insert @studentmain
select '00001', 'Mary' union all
select '00002', 'Jack'
declare @studentscore table(student_id varchar(10),test_id int,student_score int)
insert @studentscore
select '00001', 1001, 56 union all
select '00001', 1002, 78 union all
select '00001', 1003, 98
declare @subjectmain table(subject_id int,subject_name varchar(10))
insert @subjectmain
select 11111, 'Maths' union all
select 22222, 'Chinese'
declare @testschedule table(test_id int,subject_id int,test_date varchar(10))
insert @testschedule
select 1001, 11111, '2007/1/1' union all
select 1002, 11111, '2007/2/2' union all
select 1003, 22222, '2007/3/3'
----查询
SELECT
a.student_name,
d.subject_name,
MAX(e.student_score) AS student_score
FROM @studentmain AS a
CROSS JOIN (select * from @testschedule as t where t.test_date =
(select max(test_date) from @testschedule where subject_id = t.subject_id)) AS c
LEFT JOIN @subjectmain AS d ON c.subject_id = d.subject_id
LEFT JOIN @studentscore AS e ON c.test_id = e.test_id and a.student_id = e.student_id
GROUP BY a.student_name,d.subject_name
ORDER BY a.student_name DESC,student_score
/*结果:
student_name subject_name student_score
------------ ------------ -------------
Mary Maths 78
Mary Chinese 98
Jack Maths NULL
Jack Chinese NULL
*/
这样试试:
SELECT
a.student_id,
a.student_name,
d.subject_name,
b.student_score,
c.test_date
FROM studentmain as a
LEFT JOIN studentscore as b ON a.student_id = b.student_id
INNER JOIN (select * from testschedule as t where t.test_date =
(select max(test_date) from testschedule where subject_id = t.subject_id)) as c
ON b.test_id = c.test_id
LEFT JOIN subjectmain as d ON c.subject_id = d.subject_id
select distinct t1.student_name, t2.subject_name, isnull(t4.student_score,0)
from studentmain t1
left join subjectmain t2
on t1.student_id=t2.student_id
inner join (select t1.* from testschedule t1 where not exists(select 1 from testschedule t2 where t2.test_date>t1.test_date)) t3
on t2.subject_id=t3.subject_id
left join studentscore on t1.student_id=t4.student_id and t3.test_id=t4.test_id