这里的数据:SQL为每个组查找列顺序由另一列
studentid,state,score,date,modno,transactionno
1,IL,10,20170101,0,0
1,VA,20,20170102,1,1
1,FL,30,20170103,2,2
2,VA,40,20170101,0,0
2,IL,50,20170102,1,1
2,IN,60,20170103,2,2
这里的输出应该是这样:
studentid,state,score,date
1,IL,20,20170101
2,VA,60,20170102
所以基本上我们通过studentid要组。
根据最低的modno和transactionno查找第一个状态。
根据最高的modno和transactionno查找第一个分数。
select studentid,
(select top 1 state from student s1 where s.studentid = s1.studentid order by modno, transactionno) as state,
(select top 1 score from student s1 where s.studentid = s1.studentid order by modno desc, transactionno desc) as score,
(select top 1 date from student s1 where s.studentid = s1.studentid order by modno, transactionno) as date
from student s
group by studentid
以上是我在SQL 2008中的查询。是否有其他方法来编写此查询以获得更好的性能?当处理大型数据集并且每组拉出两个以上的列时,它真的会显示出来。
为什么一个'studentid'列有在'student'表中的多个行? –
我不理解输出。应该使用什么*日期*为什么20为id - 1?它不应该是30吗? – Parfait