以上都不是我想要的,我來結貼吧,
用開窗函數(不知哪位大神還有其它方法,此方法無法解決同一時間,同一倉庫,同一料號的非常規象,因只取一條,不一定ID是最大值的):
SELECT BillDate as 日期,MaterialNo as 料號 ,SHID as 倉庫,StockQty as 出入數量 FROM (
SELECT BillDate,MaterialNo ,SHID,StockQty ,ROW_NUMBER()OVER(PARTITION BY MaterialNo ,SHID ORDER BY BillDate DESC) id
from ST_StockRecord
)a WHERE id=1 order by BillDate,MaterialNo ,SHID
![圖片說明](http://img.ask.csdn.net/upload/201511/05/1446723377_86656.jpg)