简体   繁体   中英

Return all rows from LEFT table even no record in RIGHT table and maximum expiry date from RIGHT tables

thanks in advance to help me out on this, I am new to SQL Server and need your expert help, I want to return all values from LEFT table even no record in right table and MAX(MOT Expiry) or MAX(Road Tax) Expiry.

I tried this query but cant get the required results

    SELECT Vehicle.id,vehicle.registrationMark, vehicle.status, RoadTax.Expiry, MOT.Expiry
    FROM Vehicle
    LEFT JOIN RoadTax ON Vehicle.id = RoadTax.VehicleID
    LEFT JOIN MOT ON Vehicle.id = MOT.VehicleID;

For making it clear look at this example. I want to return all Vehicles from Vehicle table with their max TAX and MOT Expiry. even a record for a vehicle is not available in Road Tax or MOT Table.

Vehicle Table

    ---------------------------------
    | ID | RegistrationMark | Status |
    ---------------------------------
    |  1 | ABC              | Active |
    |  2 | DEF              | Active |
    |  3 | GHI              | Active |
    ---------------------------------

Road Tax Table

    ------------------------------
    | ID | VehicleID | Expiry     |
    ------------------------------
    |  1 | 1         | 10/10/2013 |
    |  2 | 1         | 10/10/2014 |
    |  3 | 2         | 20/12/2014 |
    ------------------------------

MOT Table

    -------------------------------
    | ID | VehicleID | Expiry      |
    -------------------------------
    | 1  | 2         | 25/01/2015  |
    -------------------------------

Result I want

    -----------------------------------------------------------
    | ID | RegistrationMark | Status | TaxExpiry  | MOTExpiry  |
    -----------------------------------------------------------
    | 1  | ABC              | Active | 10/10/2014 | NULL       |
    | 2  | DEF              | Active | 20/12/2014 | 25/01/2015 |
    | 3  | GHI              | Active | NULL       | NULL       |
    -----------------------------------------------------------

Thank you so much for your help.

Seems like you were on the right track with your query - you just need to add the aggregation:

SELECT Vehicle.id,vehicle.registrationMark, vehicle.status
, Max(RoadTax.Expiry) as LatestTax_Exp
, Max(MOT.Expiry) as LatestMOT_Exp
FROM Vehicle
LEFT JOIN RoadTax ON Vehicle.id = RoadTax.VehicleID
LEFT JOIN MOT ON Vehicle.id = MOT.VehicleID;
group by vehicle.ID, vehicle.Registrationmark, vehicle.status

or, if I have misunderstood what you wanted and vehicles can have more than one status or registration, try:

SELECT Vehicle.id,vehicle.registrationMark, vehicle.status
, Max(RoadTax.Expiry) over (partition by Vehicle.ID) as LatestRT_Exp
, Max(MOT.Expiry) over (partition by Vehicle.ID) as LatestMOT_Exp
FROM Vehicle
LEFT JOIN RoadTax ON Vehicle.id = RoadTax.VehicleID
LEFT JOIN MOT ON Vehicle.id = MOT.VehicleID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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