Skip to content
Advertisement

How can I get the last date of different activities in a access table?

Data Example

I want get the last date of each service. I would like to achieve the following:

+-------------------+---------------------+------+------------+---------------+---------------+
|        Vin        |  Concesionario_Crm  |  OTS |   Fecha    |     Service   | Concesionario |
+-------------------+---------------------+------+------------+---------------+---------------+
| JAANLR55EE7102074 |   AUTOCONSA UIO     |   9  | 23/04/2019 | MANTENIMIENTO |  AUTOCONSA UIO|
+-------------------+---------------------+------+------------+---------------+---------------+
| JAANLR55EE7102074 |   AUTOCONSA UIO     |   9  | 10/09/2018 |    MECANICA   |  AUTOCONSA UIO|
+-------------------+---------------------+------+------------+---------------+---------------+

I tried this but it just get the record when the selected service have the last date

Mantenimiento = "
    INSERT INTO SERVICIO 
    SELECT DISTINCT 
        t2.Mes_CRM, 
        t2.Razón_social, 
        t2.vin, 
        t2.concesionario_crm, 
        t1.ots,    
        t1.fecha, 
        t2.tipo_servicio, 
        t2.concesionario 
    FROM 
        (
            SELECT 
                MAX(t.Mes_CRM) AS Mes_CRM, 
                t.vin, 
                SUM(t.ots) AS ots, 
                MAX(t.fecha_salida) AS fecha 
            FROM CASO AS t 
            GROUP BY t.vin
        ) AS t1 
        INNER JOIN CASO AS t2 
            ON (t1.Mes_CRM = t2.Mes_CRM) 
            AND (t1.fecha = t2.fecha_salida) 
            AND (t1.vin = t2.vin) 
    WHERE (t2.tipo_servicio = 'MANTENIMIENTO')"

Advertisement

Answer

I think you want:

select c.*
from caso as c
where c.fecha = (select max(c2.fecha)
                 from caso c2
                 where c2.service = c.service
                );
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement