2017-07-14 67 views
0

如何为以下脚本应用CROSS APPLY函数?我想检索每个多记录的TOP 1。从多个记录中选择top 1 SQL Server

select s.UnitID 
     ,p.PartNumber 
     ,po.ProductionOrderNumber 
     ,s.[Value] as SerialNumber 
     ,p.Revision 
     ,case ush.UnitStatusID 
     when 0 
      then 'Processing' 
     else 'Completed' 
     end as UnitStatusID 
     ,ush.[Time] as LastUpdate 
     ,ush.LooperCount 
from ffpart as p 
    join ffProductionOrder as po 
     on p.ID = po.PartID 
    join ffUnitDetail as ud 
     on po.ID = ud.ProductionOrderID 
    join ffSerialNumber as s 
     on ud.UnitID = s.UnitID 
    join ffUnitStatusHistory as ush 
     on ud.UnitID = ush.UnitID 
where s.[Value] in('ACNX4853' 
       ,'ACRA3259' 
       ,'AY1712466664' 
       ) 
order by ush.[Time] desc; 

感谢您的回答..提前致谢。作为连接

+1

强调检索[不良习惯踢:使用旧样式的JOIN( http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins.aspx) - 旧式*逗号分隔的表格列表* style被替换为ANSI - ** 92 ** SQL标准中的* proper * ANSI'JOIN'语法(** 25 years ** ago),并且不鼓励使用它 –

+0

*您的意思是* TOP 1多个记录*?你如何分组数据?你只需要一个记录哪个结果?请更具体一些,理想情况下包括您正在获得的结果样本以及您试图实现的输出结果。 – iamdave

+0

现在单个记录检索多个输出。因此,我想根据LastUpdate列和[按LastUpdate列desc排序]检索最新记录, – ghaty

回答

0

脚本显示输出,我只希望这是在绿色[为了通过LASTUPDATE列降序]

Script Result