查詢結果是多行明細時,取第二行的SQL腳本

查詢結果是多行明細時,取第一行的SQL腳本

 select top 1 GoodsCode,PhaseFactory,* from TBOPhasein where  GoodsCode = 'D69588' order by BillMakeDate desc
  select top 1 PhaseFactory from TBOPhasein where  GoodsCode = 'D69588' order by BillMakeDate desc

 

 

 

查詢結果是多行明細時,取第二行的SQL腳本

 declare @LastbatchPhaseFactory varchar(10)
 
 select top 1 @LastbatchPhaseFactory = a.PhaseFactory
 from( select top 2 * from TBOPhasein where  GoodsCode = 'D69588' order by BillMakeDate desc) a
 order by a.BillMakeDate asc
 
 select isnull(@LastbatchPhaseFactory,'')

或者

 
 select top 1  a.PhaseFactory
 from( select top 2 * from TBOPhasein where  GoodsCode = 'D69588' order by BillMakeDate desc) a
 order by a.BillMakeDate asc

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章