使用查询检索当前值和先前值

时间:2019-04-02 07:55:10

标签: sql

我需要检索针对特定车辆下达的任何燃油订单的当前和以前的里程表读数。我使用了下面的查询,但以前的里程表读数未获取。我可能做错了什么?

这是我从查询中得到的结果:

doc#      doc_status     Vehicle      Fuel_type     Current       Previous
                                                    Odometer      Odometer
                                                    Reading       Reading

1902011    APPROVED   KAP 725W ISUZU  DIESEL        502199        NULL                                     
1902012    APPROVED   KBR 878U ISUZU  DIESEL        98094         NULL                               
1902013    APPROVED   KBR 877U ISUZU  DIESEL        150334        NULL                            
1902014    APPROVED   KBU 812T ISUZU  DIESEL        175166        NULL

这是查询:

SELECT
  dbo.t_doc_data.docNo,
  dbo.t_doc_data.docstatus,
  dbo.t_doc_fuelorders.fovid,
  dbo.t_fuel_order_vehicles.vregno,
  dbo.t_doc_fuelorders.fofueltype,
  (SELECT
    MAX(foodometer)
  FROM dbo.t_doc_fuelorders
  WHERE docNo = dbo.t_doc_data.docNo
  AND fovid = t_doc_fuelorders.fovid)
  current_odometer_reading,
  (SELECT
    MAX(foodometer)
  FROM dbo.t_doc_fuelorders
  WHERE docNo = dbo.t_doc_data.docNo
  AND fovid = t_doc_fuelorders.fovid
  AND foodometer < (SELECT
    MAX(foodometer)
  FROM dbo.t_doc_fuelorders
  WHERE docNo = dbo.t_doc_data.docNo
  AND fovid = t_doc_fuelorders.fovid))
  previous_odometer_reading
FROM dbo.t_doc_data
INNER JOIN dbo.t_doc_fuelorders
  ON dbo.t_doc_data.docNo = dbo.t_doc_fuelorders.docNo
INNER JOIN dbo.t_fuel_order_vehicles
  ON dbo.t_doc_fuelorders.fovid = dbo.t_fuel_order_vehicles.vid

0 个答案:

没有答案