SELECT
v.id as vid,v.device_id,v.name as vhc_name
,c.name as vhc_cat_name,t.name as vhc_type_name
,v.nopol1,v.nopol2,v.nopol3
,vd.fvhc_img
,v.is_track_holiday,v.track_sch_d,v.track_sch_h,vd.speed_limit,v.crt as vhc_crt
,client.id as client_group_id,client.c_name as client_group_name
,tr.ignition,tr.stts_engine,tr.stts_gps,tr.stts_gsm
,tr.pre_milleage,tr.sum_milleage,tr.vhc_milleage,v.sum_milleage AS vhc_sum_milleage_1
,(SELECT SUM(pre_milleage) FROM db_trucking.t_gps_tracks WHERE vhc_id = v.id LIMIT 1) as vhc_sum_milleage
,(SELECT crt_s FROM db_trucking.t_gps_tracks WHERE stts_engine = 1 AND vhc_id = v.id AND crt_s >= (
SELECT crt_s FROM db_trucking.t_gps_tracks WHERE stts_engine IN (3,2) AND vhc_id = v.id AND crt_s <= tr.crt_s ORDER BY id DESC LIMIT 1
) ORDER BY id ASC LIMIT 1) as lst_idle_at
,(SELECT crt_s FROM db_trucking.t_gps_tracks WHERE stts_engine = 3 AND vhc_id = v.id AND crt_s >= (
SELECT crt_s FROM db_trucking.t_gps_tracks WHERE stts_engine IN (1,2) AND vhc_id = v.id AND crt_s <= tr.crt_s ORDER BY id DESC LIMIT 1
) ORDER BY id ASC LIMIT 1) as lst_stop_at
,tr.id AS lst_master_id,tr.latitude AS lst_lat,tr.longitude AS lst_lng,tr.speed AS lst_speed,tr.orientation AS lst_orientation
,tr.crt AS lst_loc_crt,tr.crt_d AS lst_loc_crt_d,tr.crt_s AS lst_loc_crt_s
,tr_addr.master_id AS lst_addr_master_id,tr_addr.country_text AS lst_country_text,tr_addr.state_text AS lst_state_text,tr_addr.city_text AS lst_city_text
,tr_addr.district_text AS lst_district_text,tr_addr.village_text AS lst_village_text,tr_addr.postcode AS lst_postcode
,tr_addr.streets AS lst_streets,tr_addr.fulladdress AS lst_fulladdress
,(SELECT COUNT(id) FROM db_trucking.t_gps_tracks WHERE vhc_id = v.id AND action = 'heartbeat' AND crt BETWEEN 1668583927 AND 1668584527 LIMIT 1) as lst_heartbeat
,ord.id as ord_id,ord.code as ord_code,ord.status as ord_stts,ord.crt as ord_crt
,ord_pck.pck_name as ord_pck_name,ord_pck.pck_addr as ord_pck_addr,ord_drop.drop_name as ord_drop_name,ord_drop.drop_addr as ord_drop_addr
,(SELECT nmKotamadyaKel FROM db_trucking.t_region WHERE kodeKab = ord_pck.pck_ktid LIMIT 1) ord_pck_ktname
,(SELECT nmKotamadyaKel FROM db_trucking.t_region WHERE kodeKab = ord_drop.drop_ktid LIMIT 1) ord_drop_ktname
,ord_drv.drv_name as ord_drv_name,ord_drv.drv_phone_val as ord_drv_phone_val,ord_drv.drv_phone2_val as ord_drv_phone2_val,ord_drv.drv_addr as ord_drv_addr
,ord_c.c_name as ord_c_name,ord_c.c_pt_name as ord_c_pt_name

FROM db_trucking.t_vehicles AS v

INNER JOIN db_trucking.t_vehicles_detail AS vd ON v.id = vd.vid
INNER JOIN db_trucking.t_vehicles_types AS t ON v.type_id = t.id
INNER JOIN db_trucking.t_vehicles_cats AS c ON v.cat_id = c.id
LEFT JOIN db_trucking.t_users AS vendor ON v.vendor_id = vendor.id
LEFT JOIN db_trucking.t_clients AS client ON vendor.client_group_id = client.id

-- LEFT JOIN ( SELECT MAX(crt) max_crt, device_id FROM db_trucking.t_gps_tracks WHERE latitude is not null AND longitude is not null GROUP BY device_id ORDER BY crt DESC ) AS tr1 ON (v.device_id = tr1.device_id)
-- LEFT JOIN db_trucking.t_gps_tracks AS tr ON (tr.crt = tr1.max_crt)
-- LEFT JOIN db_trucking.t_gps_tracks AS tr ON tr.vhc_id = v.id
LEFT JOIN db_trucking.t_gps_tracks_rltm AS tr ON tr.vhc_id = v.id

LEFT JOIN db_trucking.t_gps_tracks_address AS tr_addr ON (tr.id = tr_addr.master_id)
LEFT JOIN db_trucking.t_orders_vehicles as ord_vhc ON v.id = ord_vhc.vhc_id
LEFT JOIN db_trucking.t_orders as ord ON ord_vhc.ord_id = ord.id
LEFT JOIN db_trucking.t_orders_pickups as ord_pck ON ord_vhc.ord_id = ord_pck.ord_id
LEFT JOIN db_trucking.t_orders_drops as ord_drop ON ord_vhc.ord_id = ord_drop.ord_id
LEFT JOIN db_trucking.t_orders_drivers as ord_drv ON ord_vhc.ord_id = ord_drv.ord_id
LEFT JOIN db_trucking.t_orders_clients as ord_c ON ord_vhc.ord_id = ord_c.ord_id
WHERE v.dlt is null
AND tr.latitude != 0 AND tr.latitude is not null
GROUP BY v.id
ORDER BY tr.crt_d DESC LIMIT 500;



