从多个记录SQL Server中选择前1

时间:2017-07-14 09:02:01

标签: sql-server-2008

如何对以下脚本应用CROSS APPLY函数?我想为每个多个记录检索TOP 1。

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 个答案:

答案 0 :(得分:0)

脚本显示输出为附加,我只想检索哪个以绿色突出显示[按 LastUpdate 列desc排序]

Script Result