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
它工作正常,但不是我想要的。它给了我Intensity
Segment_ID.Segment_ID = Intensity.Segment_ID
的第一行,但我想要获取最新一行。
在表格Intensity
中,我的日期列的日期类似于此2009-02-20 15:00:00.000
如何使用最新日期对Segment_ID
行进行排序?
答案 0 :(得分: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