开发者

SQL Query to obtain Trip Details

I have a table which has columns like StartDate, EndDate, Registration_No, Start_Odometer, End_Odometer, Stop_time, Running_Time, Idle_Time, Distance, StartOPID, EndOPID ..

StartOPID, EndOPID can have values like 5 or 1. I consider a trip when StartOPID=5 and EndOPID=5. A Vehicle can have many trips on the same date but the timings differ. I need to write a query where I can get the Trip details of a particular vehicle during the day which has StartOPID=5 and EndOPID=5 & also I need to get the total distance, stoptime,idletime during this trip..

I have written a query but its not giving me the desired result..plz help..

select
  Registration_No,
  StartDate,
  EndDate,
  Start_Odometer,
  End_Odome开发者_StackOverflowter,
  sum(Total_Trip_Time) as Total_Trip_Time,
  sum(Idle_Time)       as Idle_Time,
  sum(Stop_Time)       as Stop_Time,
  sum(Running_Time)    as Running_Time,
  sum(Distance)        as Distance
from dbo.Trip_Summary
where System_Id       = ?
  and Client_Id       = ?
  and Registration_No = ?
  and StartDate between ? and ?
  and((StartOPID=5 and EndOPID=1) or (StartOPID=1 and EndOPID=5))
group by StartDate, Registration_No, EndDate, Start_Odometer, End_Odometer 
order by StartDate, Registration_No


In my opinion, you might want to remove Start_Odometer and End_Odometer from group by clause. I assume each trip will have different start_odometer and end_odometer. If so, you will not get total trip time etc., per vehicle on a day. You will get total trip time per trip.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜