crossing 2 tables and get the last record by date in sql

1

query I have 2 tables A and B; IN THE TABLE I REQUIRE TO BRING THE FIELD DATE_RESPOSED FROM TABLE B, THE CONDITION IS BY THE LAST DATE OF RESPONSE OF TABLE B

TABLE A

ID - DATE_HEADING - PRODUCT

3349793 - 2018-01-07 18:08 - A

3356512 - 2018-01-11 10:18 - B

3343136 - 2018-01-03 15:12 - C

3343207 - 2018-01-03 15:41 - D

3343708 - 2018-01-03 21:33 - E

TABLE B

ID - DATE_RESPOSED - PRODUCT

3349793 - 2018-01-08 17:34 - A

3349793 - 2018-01-08 17:37 - A

3356512 - 2018-01-11 18:02 - B

3356512 - 2018-01-12 09:09 - B

3343136 - 2018-01-04 16:16 - C

3343207 - 2018-01-04 16:32 - D

3343708 - 2018-01-04 18:15 - E

    
asked by Jsebast 26.02.2018 в 01:44
source

1 answer

0

If I did not misunderstand your question, the answer goes through a LEFT or possibly a INNER JOIN between the two tables. In the first case you would bring all the records of table A plus the records of table B that match by ID , in the case of INNER only records whose ID are in both tables.

Regarding the date, you should use the aggregation function MAX on FECHA_RESPUESTA and group the rest of the columns

SELECT A.ID,
       A.FECHA_INGRESO,
       A.PRODUCTO,
       MAX(B.FECHA_RESPUESTA)
       FROM TablaA A
       LEFT JOIN TablaB B
           ON A.ID = B.ID
       GROUP BY A.ID,
                A.FECHA_INGRESO,
                A.PRODUCTO
    
answered by 27.02.2018 в 04:07