0

SQL Server 中有一个查询:

SELECT  
    Segment_ID = Segment_ID.Segment_ID,
    GEOMETRY::STGeomFromText(Track.Track.STAsText(),4326) as the_geom,
    a.NumAll,
    a.AverageDailyIntens,
    a.AverageDailyIntensCar,
    a.Loading   
FROM dbo.Segment_ID
RIGHT JOIN Road  ON Segment_ID.Road_ID = Road.Road_ID
LEFT JOIN Track ON Segment_ID.Segment_ID = Track.Segment_ID
outer apply (select top 1 Intensity.NumAll,Intensity.AverageDailyIntens,Intensity.AverageDailyIntensCar,Intensity.Loading   
from Intensity 
         where Segment_ID.Segment_ID = Intensity.Segment_ID
         ORDER BY Intensity.NumAll) a

它工作正常,但不是我想要的。它给了我第一排IntensitySegment_ID.Segment_ID = Intensity.Segment_ID但我想拿最新排。

在表中Intensity,我有日期列,日期如下2009-02-20 15:00:00.000
我怎样才能Segment_ID用最新的日期排到需要的位置?

4

1 回答 1

1

尝试更改ORDER BY子句以按日期列降序排序:

outer apply (
   select top 1
          Intensity.NumAll
        , Intensity.AverageDailyIntens
        , Intensity.AverageDailyIntensCar
        , Intensity.Loading   
   from Intensity 
   where  Segment_ID.Segment_ID = Intensity.Segment_ID
   ORDER BY Intensity.your_date_field DESC
   ) a
于 2013-03-31T18:48:38.723 回答