【发布时间】:2017-10-28 14:01:46
【问题描述】:
这是数据:
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
所以基本上我们想按学生号分组。
根据最低的 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吗?
标签: sql sql-server sql-server-2008 group-by aggregate