0
I’m trying to make a SELECT
so that it displays from the second table only the most recent record.
I tried to do it using GROUP BY
and ORDER BY
, but with the GROUP BY
mine SELECT
does not work, only with the ORDER BY
, tried to use SELECT TOP 1
but the result did not go as expected, displaying only the most recent record of all.
Tabela A = ID_A , Produto, Tipo | Tabela B = ID_A, ID_B, Descricao, dt_renovacao
The tables are associated and I am trying to select the id, product and type of the Table A and together, id, Description and dt_renewal of the Table B, using as criterion, the record with the most recent dt_renewal of table B.
That is, for each record of Table A only a single record of Table B will be displayed which should be the most recent.
The code I made is this:
SELECT A.id_a, A.produto, A.tipo, B.id_B, B.descricao, B.dt_renovacao
FROM A INNER JOIN B ON A.id_a = B.id_a GROUP BY B.id_a ORDER BY B.dt_renovacao DESC
id_a and id_B are primary keys?
– Marcos Barbosa
Both are primary and auto increment
– gustavo.491