繁体   English   中英

改进内连接/临时表查询

[英]Improve Inner-Join/Temp Table Query

我有这个查询,它返回了我期望的结果,但是,正如您所看到的,它非常粗糙且难以阅读。

我尝试了 Inner Joins 和 FIRST_VALUE 方法,但没有得到正确的结果,所以求助于临时表(只是因为我发现这样更容易验证结果)。

问题是我从 dbo.Trips 获得了 StartWayPoint 和 EndWayPoint,这实际上只是输入 GPSWaypoints 表的第一个和最后一个点(对于特定的旅行/车辆)。

这些点不是按 UTCTime 顺序输入的,所以我需要取 StartWayPoint、EndWayPoint 和 Vehicle,从 dbo.GPSWaypoints 表中获取这些航点值之间的结果,按 UTCTime desc 排序,第一个 Location 字符串是我真正的 EndLocationString。

希望这是有道理的,我可能把它复杂化了....我觉得答案是内部联接或子查询,但我的 SQL 技能并不那么热门,因此感谢任何帮助。

DROP TABLE IF EXISTS #temp1, #temp2

USE         XXX

DECLARE     @StartWayPoint bigint, @EndWaypoint bigint, @Vehicle smallint, @TripId smallint = 9863;

SELECT      t.Id, 
            t.Date, 
            t.StartWayPoint, 
            t.EndWayPoint, 
            t.Distance, 
            t.Alarms, 
            t.FuelConsumption, 
            t.Vehicle, 
            gpsStart.UtcTime as TripStart, 
            gpsEnd.UtcTime as TripEnd, 
            gpsStart.LocationString as StartLocationString, 
            gpsEnd.LocationString as EndLocationString
INTO        #temp1
FROM        dbo.Trips t 
LEFT JOIN   dbo.GPSWaypoints gpsStart on StartWaypoint = gpsStart.Id 
LEFT JOIN   dbo.GPSWaypoints gpsEnd on EndWaypoint = gpsEnd.Id 
LEFT JOIN   dbo.Operators o on Driver = o.Id 
LEFT JOIN   dbo.Vehicles v on t.Vehicle = v.Id 
WHERE       t.id = @TripId


SELECT      @StartWayPoint = (SELECT StartWaypoint FROM #temp1), @EndWaypoint = (SELECT EndWaypoint FROM #temp1), @Vehicle = (SELECT Vehicle FROM #temp1)

SELECT TOP 1 g.Id, 
            g.LocationString, 
            g.Vehicle
INTO        #temp2
FROM        dbo.GPSWaypoints g
WHERE       Id BETWEEN @StartWayPoint AND @EndWaypoint 
AND         Vehicle = @Vehicle 
order by    UtcTime desc

SELECT      t1.*,
            t2.Id as TRUE_EndWayPoint,
            t2.LocationString as TRUE_EndLocationString
FROM        #temp1 t1
LEFT JOIN   #temp2 t2 on t2.Vehicle = t1.

#temp1 的结果

#temp1 的结果

#temp2 的结果(带有 TRUE EndWayPoint 和 EndLocationString

#temp2 的结果(带有 TRUE EndWayPoint 和 EndLocationString

认为我已经用嵌套查询为自己回答了它,在我之前的尝试中一定只是出错了……但仍然愿意接受改进建议。

select  t.Id, 
        t.Date, 
        t.StartWayPoint, 
        t.EndWayPoint, 
        t.Distance, 
        t.Alarms, 
        t.FuelConsumption,      
        t.Vehicle,          
        gpsStart.UtcTime as TripStart, 
        gpsEnd.UtcTime as TripEnd, 
        gpsStart.LocationString as StartLocationString, 
        (   select top 1 (g.LocationString) 
            from dbo.GPSWaypoints g 
            where Id BETWEEN gpsStart.Id AND gpsEnd.Id 
            AND Vehicle = v.Id 
            order by UtcTime desc
        ) as EndLocationString
from    dbo.Trips t 
left join dbo.GPSWaypoints gpsStart on StartWaypoint = gpsStart.Id 
left join dbo.GPSWaypoints gpsEnd on EndWaypoint = gpsEnd.Id 
left join dbo.Operators o on Driver = o.Id 
left join dbo.Vehicles v on t.Vehicle = v.Id 
where   t.id = 9863

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM