如图,行转列,并且取最新时间的数据。期待结果,sql如下,不知道如何优化一下
select a.score as '语文',a.name, g.score as '数学' FROM score a
INNER JOIN(
select max(b.time)as time,name from score b WHERE b.class='语文'
group by b.name) c
on a.name = c.name and a.time = c.time
left JOIN(
SELECT d.score,d.name,d.class from score d
INNER JOIN(
select max(e.time) as time,name from score e where e.class='数学' group by e.name
) f
on d.name = f.name and d.time = f.time) as g on a.name = g.name