只选择一行具有最大日期时间

时间:2014-04-11 07:30:30

标签: sql datetime row max

SELECT     v.DoorNo,v.NewDoorNo, v.GarageName, c.VersionDate, v.OperatorName as Operator, v.OperatorId, c.DeviceId, c.VehicleId, c.ProgramVersionNo, 
                  c.ParameterFileVersionNo, c.TariffFileVersionNo, c.BlaclistFileVersionNo, c.LineNoFileVersionNo, c.ScreenProgramVersion, 
                  c.SoapProgramVersiyon
FROM         dbo.vValidator AS v RIGHT JOIN
                  dbo.CDeviceVersion AS c  with(nolock) ON (v.DoorNo = c.DoorNo
                                or v.NewDoorNo=c.DoorNo
                                                          where v.NewDoorNo='O1211'

此查询返回2行,但我想只选择一行具有最大版本日期。我该如何选择它?

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

SELECT  TOP (1) v.DoorNo,v.NewDoorNo, v.GarageName, c.VersionDate, v.OperatorName as Operator, v.OperatorId, c.DeviceId, c.VehicleId, c.ProgramVersionNo, 
                  c.ParameterFileVersionNo, c.TariffFileVersionNo, c.BlaclistFileVersionNo, c.LineNoFileVersionNo, c.ScreenProgramVersion, 
                  c.SoapProgramVersiyon
FROM         dbo.vValidator AS v RIGHT JOIN
                  dbo.CDeviceVersion AS c  with(nolock) ON (v.DoorNo = c.DoorNo
                                or v.NewDoorNo=c.DoorNo)
                                                          where v.NewDoorNo='O1211'
 ORDER BY c.VersionDate