SQL查询多个分类最新数据并求出较前一数据的变化

--Change为自定义较上期的变化值
select NoteID,code,UpdateTime,Balance
,Change=Balance-
(
--根据主查询t1得出的代码和时间作为条件获得小于最大日期的上期max(UpdateTime)日期的额度Balance值
select Balance from SMS_FillMonenyNote where code=t1.code and 
UpdateTime=(select max(UpdateTime) from SMS_FillMonenyNote 
where code=t1.code and UpdateTime<t1.UpdateTime)
from SMS_FillMonenyNote as t1 
--根据not exists找出不存在有大于NoteID的t1.NoteID,也就是最大的NoteID
where not exists (select * from SMS_FillMonenyNote where code=t1.code
and NoteID>t1.NoteID)
order by code
posted on 2012-10-10 16:50  Gary.han  阅读(1117)  评论(0编辑  收藏  举报